First of all, I'm still a beginner, so I'd appreciate it if you could have some patience :)
I've been scratching my head so hard today about this.
The thing is, I want to run three different backgroundworkers. But I would like to wait until one has finished to run the next one, and so on.
Each backgroundworker requires time to finish. Long story short, the thing is, I'm using WaitOne() so whenever the previous backgroundworker tells me it's done, I could start running the new one, but the UI thread is freezing until the three backgroundworkers finish.
I'm seting up a different AutoResetEvent, which is the one taking care of the whole waiting and running thing.
Here's the code:
AutoResetEvent _resetRIEvent = new AutoResetEvent(false);
AutoResetEvent _resetHEvent = new AutoResetEvent(false);
AutoResetEvent _resetSEvent = new AutoResetEvent(false);
await Task.Run(() => bgwTestResInd.RunWorkerAsync());
_resetRIEvent.WaitOne();
await Task.Run(() => bgwTestHipot.RunWorkerAsync());
_resetHEvent.WaitOne();
await Task.Run(() => bgwTestSurge.RunWorkerAsync());
_resetSEvent.WaitOne();
MessageBox.Show("Im done for real.");
Also, is it allowed to do this?
private void bgwTestResInd_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_resetRIEvent.Set();
}
private void bgwTestHipot_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_resetHEvent.Set();
}
private void bgwTestSurge_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_resetSEvent.Set();
}