Your code seems to be rather confused. It refers to a variable which doesn't exist (
FrequencyPeriod
). It doesn't use the loop variable (
Frequency
). It takes a copy of another variable "for closure", but never uses that copy. Each thread waits for a manual reset event which is never set. On each iteration of the loop, you start the current thread, then start all of the threads, then wait for all of the threads to finish.
Try using
Parallel.ForEach
instead:
Parallel.ForEach(GetAllProcessing.ToList(), frequency =>
{
ProcessEngine.ProcessEngine objServerLevelProcessing = new SalaryProcessEngine.SalaryProcessEngine(null, "", frequency);
objServerLevelProcessing.Execute();
});
Parallel.ForEach Method (System.Threading.Tasks) | Microsoft Docs[
^]
Write a simple parallel program using Parallel.ForEach | Microsoft Docs[
^]