开发者

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 d开发者_运维百科elegates 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


If you are using three BackgroundWorkers, you can use the event RunWorkerCompleted to notice that one of the workers is completed: Before starting the workers set a counter to 3 then decrement and check this counter in the method called by RunWorkerCompleted if it hits 0 you are finished.


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!


If you just want to poll the worker threads, you could use something like bool threadWasDone = thread.Join(0);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜