I have two methods which do some work by using BackgrounderWorker. If i run first method then it does some work in background and same goes for second method. But what I want is that second method should wait untill method one worker is started.
Code
private void Method1(object sender, RoutedEventArgs e)
{
RunFirstWorker();
}
private void Method2(object sender, RoutedEventArgs e)
{
RunFirstWorker();
RunSecondWorker();
}
You could use event object (
ManualResetEventorAutoResetEvent) to synchronise execution of these functions. Create the event in non-signalled state and wait for it at the beginning ofRunSecondWorker()‘sDoWorkevent handler. Set the state of this event to signalled at the end ofRunFirstWorker()‘sDoWorkevent handler.In your original code snippet
Method1()andMethod2()were GUI callbacks (methods executed on the GUI thread) so you don’t want to block them (you don’t want to wait for event in any of those methods themselves but in BackgroundWorker callback –DoWorkevent handler).