Re: Why the "RunWorkerCompleted" never comes after pressing ENTER?
On Tue, 04 Aug 2009 17:31:01 -0700, senglory
<senglory@discussions.microsoft.com> wrote:
[...]
static void bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 10000; i++){ Thread.Sleep(1000);
Console.WriteLine(i);
if (e.Cancel){e.Result = "done"; return ;}} }
}
}
If to hit Enter after running, the bw_RunWorkerCompleted() will be never
invoked. Why?
Because your DoWork event handler never knows that you have tried to
cancel the worker.
The DoWorkEventArgs.Cancel property is for writing, not reading. You need
to check the CancellationPending property of the BackgroundWorker to see
if you should cancel. If you find it set to "true", then _your_ code
should set the DoWorkEventArgs.Cancel property so that the client can tell
that the worker was in fact cancelled.
See below for a fixed version of your code example that works as you would
expect.
Pete
using System;
using System.ComponentModel;
using System.Threading;
namespace TestConsoleBWRunWorkerCompleted
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.RunWorkerAsync();
Console.WriteLine("Press ENTER to end worker...");
Console.ReadLine();
bw.CancelAsync();
Thread.Sleep(5000);
}
static void bw_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
Console.WriteLine("executing RunWorkerCompleted handler");
if (e.Cancelled)
{
Console.WriteLine("worker was cancelled");
}
}
static void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = (BackgroundWorker)sender;
for (int i = 0; i < 10000; i++)
{
Thread.Sleep(1000);
Console.WriteLine(i);
if (bw.CancellationPending)
{
e.Cancel = true;
e.Result = "done";
return;
}
}
}
}
}