I’m using a backgroundworker to run a separate thread, however it seems that my program is halting after the call to RunWorkerAssync().
Here’s where I set everything up:
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = false;
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(bw_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
worker.RunWorkerAsync();
console.writeline("I'm Done!");
And here is my bw_DoWork function:
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(5000);
}
And my bw_RunWorkerCompleted and bw_ProgressChanged:
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
}
else if (!(e.Error == null))
{
}
else
{
}
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
The problem I am having is that sure enough, I don’t get the console output until 5 seconds after it runs. When I change the sleep to a larger/shorter time, the wait for the console output adjusts accordingly.
Isn’t the point of using a background worker so that you can shove some task to a different thread while your program continues to execute? Or am I missing something?
Thanks
The simple application below works exactly as expected for me. It writes “I’m Done!” immediately and then “Completed” 5 seconds later. Can you think of anything unusual you may be doing in your program?