Multiple threads running ok in debug but not with exe

32 views
Skip to first unread message

jjj...@gmail.com

unread,
May 4, 2026, 8:09:43 AMMay 4
to Migrated By Firefly
Hello,

I'm trying to run multiple threads and followed the manual from https://doc.fireflymigration.com/using-threads-to-improve-performance-of-business-process.html. This is working in debug mode, but if I try to start it from the exe it looks like the started threads keep on running. 

Has anyone else seen this problem and/or knows how to solve it ?

Best regards,

Jeroen Bos
Lekkerland Nederland BV

jjj...@gmail.com

unread,
May 5, 2026, 3:16:48 AMMay 5
to Migrated By Firefly
I've found the cause of the problem. To run the threads correctly I already added a variable to the ApplicationCore, OnLoad and OnStart to indicate that they had already been executed. However we also have some logic in the OnEnd of the  ApplicationCore that shouldn't execute in the started threads. So Now I have to figure out something so that the OnEnd of the ApplicationCore will only execute when the Main thread is closed.

Op maandag 4 mei 2026 om 14:09:43 UTC+2 schreef jjj...@gmail.com:

Noam Honig

unread,
May 5, 2026, 8:16:19 AMMay 5
to jjj...@gmail.com, Migrated By Firefly
There's  a method in common called IsMainContext - make it public and use it

Noam Honig  
Founder & CEO


--
You received this message because you are subscribed to the Google Groups "Migrated By Firefly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to migrated-by-fir...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/migrated-by-firefly/96379b07-ddff-4a3f-ba6f-48fa3bb04ea9n%40googlegroups.com.

jjj...@gmail.com

unread,
May 5, 2026, 9:50:05 AMMay 5
to Migrated By Firefly
Hi Noam,

Thank you for your answer, I've solved it another way:

1. I defined a global numeric variable "Glb_Main_Thread_ID.Value"
2. In the OnStart, in the part that will only be executed once, of the ApplicationCore, I initialize this variable with System.Threading.Thread.CurrentThread.ManagedThreadId.
3. In the OnEnd the code will only be executed if "Glb_Main_Thread_ID.Value" > 0

Op dinsdag 5 mei 2026 om 14:16:19 UTC+2 schreef no...@fireflymigration.com:

Lawrence Fisher

unread,
May 5, 2026, 10:48:23 AMMay 5
to jjj...@gmail.com, Migrated By Firefly
Probably what IsMainContext does as well

Sent from my iPhone

Lawrence

On 5 May 2026, at 16:50, jjj...@gmail.com <jjj...@gmail.com> wrote:

Hi Noam,

jjj...@gmail.com

unread,
May 7, 2026, 8:10:06 AMMay 7
to Migrated By Firefly
If someone else wants to use parallel execution, it might be also worth looking into the "Parallel" class, certainly when loops come into play, then you can make make use of the methods ForEach or For. Manual thread control isn't necessary anymore. For example, this peace of code:

internal void RunParallel(string source, List<int> debiteurenLijst, string assortimentOfVoorverkoop)
{
    int cycles = debiteurenLijst.Count;
    var threads = new List<Thread>();
    int i = 1;
    string typeVerwerking = assortimentOfVoorverkoop.ToUpper() == "A" ? "assortiment" : "voorverkoop";
    Process myProcess = Process.GetCurrentProcess();

     while (i <= cycles)
    {
        myProcess.Refresh();
     
        if (threads.FindAll(t => t.IsAlive).Count < maxThreads)
        {
            var thread = ENV.Common.RunOnNewThread(() =>
            {
                Create<WebInterfaces.IV509V_IntershopBepaalPrijsParallel>().Run(
                    debiteurenLijst[i - 1],
                    Hlp_PRM_Max_Dagen_Verkocht,
                    Hlp_PRM_Max_Dagen_Onverkocht,
                    Hlp_PRM_Historie_VPK_O_Maanden,
                    false,
                    assortimentOfVoorverkoop.ToUpper()
                );
            }, false);
            thread.Name = $"{Hlp_Programmanaam} {source} Debiteur: {debiteurenLijst[i - 1]}";
            threads.Add(thread);
            u.Delay(2);
            i++;
        }
    }

     while (threads.Find(t => t.IsAlive) != null)
    {
    }
}

Can be replaced by this peace

internal void RunParallel(string source, List<int> debiteurenLijst, string assortimentOfVoorverkoop)
{
    string typeVerwerking = assortimentOfVoorverkoop.ToUpper() == "A"
        ? "assortiment"
        : "voorverkoop";

    var options = new ParallelOptions
    {
        MaxDegreeOfParallelism = maxThreads
    };

    Parallel.ForEach(debiteurenLijst, options, debiteur =>
    {
        Create<WebInterfaces.IV509V_IntershopBepaalPrijsParallel>().Run(
            debiteur,
            Hlp_PRM_Max_Dagen_Verkocht,
            Hlp_PRM_Max_Dagen_Onverkocht,
            Hlp_PRM_Historie_VPK_O_Maanden,
            false,
            assortimentOfVoorverkoop.ToUpper()
        );
    });
}

And has the following advantages:
  • Handles concurrency limit
  • Uses ThreadPool
  • No manual tracking
  • Much shorter and safer
Op dinsdag 5 mei 2026 om 16:48:23 UTC+2 schreef Lawrence:
Reply all
Reply to author
Forward
0 new messages