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: