Make sure all semaphore threads are complete.

I have a Semaphore that goes through a list of Job objects.

Here is a sample code:

List<Job> jobList = jobQueue.GetJobsWithStatus(status);
if (jobList.Count > 0)
{
  foreach (Job job in jobList)
  {
    semaphore.WaitOne();

    // Only N threads can get here at once
    job.semaphore = semaphore;
    ThreadStart threadStart = new ThreadStart(job.Process);

    Thread workerThread = new Thread(threadStart);
    workerThread.Start();
  }

  //check that every thread has completed before continuing
}

Each job object has the status member variablea (0: new, 1: inProgress, 2: completed, 3: completed withErrors).

I was wondering what is the best way to verify that all tasks in the list are complete before continuing with the script.

Note. . I tried before with ThreadPool and WaitAll, but this allows me to have a limited number of threads (no side effects) like a semaphore, and I also hit the restriction on the size of the ManualResetEvent array because it is a Windows form.

+3
source share
1 answer

Thread.Join foreach.

, : , - .

+1

Source: https://habr.com/ru/post/1736101/


All Articles