Wait for QueueUserWorkItem to Complete

2020-01-29 05:02发布

If I add jobs to the thread pool with QueueUserWorkItem... how do I keep my program from going forward until all jobs are completed?

I know I could add some logic to keep the app from running until all jobs are completed, but I want to know if there is something like Thread.Join() or if there's any way to retrieve each thread that is being assigned a job.

3条回答
太酷不给撩
2楼-- · 2020-01-29 05:25

The best way to do this is to use the CountdownEvent class. This is a fairly well established pattern and is about as scalable as it gets.

using (var finished = new CountdownEvent(1))
{
  foreach (var workitem in workitems)
  {
    var capture = workitem; // Used to capture the loop variable in the lambda expression.
    finished.AddCount(); // Indicate that there is another work item.
    ThreadPool.QueueUserWorkItem(
      (state) =>
      {
        try
        {
          ProcessWorkItem(capture);
        }
        finally
        {
          finished.Signal(); // Signal that the work item is complete.
        }
      }, null);
  }
  finished.Signal(); // Signal that queueing is complete.
  finished.Wait(); // Wait for all work items to complete.
}
查看更多
\"骚年 ilove
3楼-- · 2020-01-29 05:31

You can use .NET's Barrier class to achieve this.

Barrier barrier = new Barrier(3); 
for(int i = 0; i < 2; i++)
{
    ThreadPool.QueueUserWorkItem(
    (state) =>
    {
       foo();
       barrier.SignalAndWait();
    }, null);
}
barrier.SignalAndWait();
查看更多
干净又极端
4楼-- · 2020-01-29 05:35

You could use events to sync. Like this:

private static ManualResetEvent resetEvent = new ManualResetEvent(false);

public static void Main()
{
    ThreadPool.QueueUserWorkItem(arg => DoWork());
    resetEvent.WaitOne();
}

public static void DoWork()
{
    Thread.Sleep(5000);
    resetEvent.Set();
}

If you don't want to embed event set into your method, you could do something like this:

var resetEvent = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(
    arg => 
    {
        DoWork();
        resetEvent.Set();
    });
resetEvent.WaitOne();

For multiple items:

var events = new List<ManualResetEvent>();

foreach(var job in jobs)
{   
    var resetEvent = new ManualResetEvent(false);
    ThreadPool.QueueUserWorkItem(
        arg =>
        {
            DoWork(job);
            resetEvent.Set();
        });
    events.Add(resetEvent);
}
WaitHandle.WaitAll(events.ToArray());
查看更多
登录 后发表回答