简体   繁体   中英

How do you notify a parent thread that all child threads have terminated?

I have a console app that I'm porting to WPF. The application has 3 worker threads, that are all joined to the main thread before some output results are printed to the screen. My understanding is that, if I try and do the same thing in a WPF application, the GUI will be blocked and will not be reponsive to the user. How then can I notify the parent thread that all the threads have completed their work? I think the solution is going to involve delegates and events (or maybe BackgroundWorker?), but it was not clear to me how to get the callback invoked when the thread terminated.

Original Code:

foreach (Thread t in threadList)
{
                t.Start();
}

foreach (Thread t in threadList)
{
                t.Join();
}

// print some results here

如果您使用的是三个BackgroundWorker ,则可以使用RunWorkerCompleted事件来注意其中一个worker已完成:在启动worker之前将计数器设置为3然后在RunWorkerCompleted调用的方法中递减并检查此计数器,如果它达到0完了。

You should use three BackgroundWorkers.

You can then handle their RunWorkerCompleted events to find out when the operations finish.

Take a look at this article in MSDN magazine that gives you an example on using BackgroundWorker. Start at Figure 7.

Depends on what you would like to accomplish. What form of communication are you trying to facilitate?

If I were to guess, what you really want is to simply report [or display] your worker results in your application. If this is the case, then in a typical WPF application you have a view model, say

public class AwesomeViewModel : INotifyPropertyChanged
{
    // if small fixed number, otherwise, you could use 
    // an ObservableCollection<T> 
    public string WorkerResultA { ... }
    public string WorkerResultB { ... }
    public string WorkerResultC { ... }
}

which is data-bound to your WPF controls. You can simply pass a reference of your view model to each worker thread and they update the class without requiring blocking\\waiting on Gui thread. In this manner, each worker reports its results when it completes without intervention from anyone else. This is optimal.

Of course, if you go ahead and do just this, you run into another completely different issue . Which, fyi, is resolvable via Dispatcher. One possible solution here .

As for BackgroundWorker versus explicit Thread control, that is up to you. There are advantages to both, but remember you already have functional code written. That, and in my personal opinion, BackgroundWorker isn't particularly useful.

If you really absolutely positively must implement a more sophisticated synchronization model, then I highly recommend you brush up on ManualResetEvent its cousin AutoResetEvent , Semaphore , keyword lock and concurrent programming in general. Sorry, no shortcuts there :)

Hope this helps!

如果你只想轮询工作线程,你可以使用像bool threadWasDone = thread.Join(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM