Why the "RunWorkerCompleted" never comes after pressing ENTER?

  • Thread starter Thread starter senglory
  • Start date Start date
S

senglory

using System;
using System.ComponentModel ;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;


namespace Retriever
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;


bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
bw.CancelAsync();
Thread.Sleep(5000);
}

static void bw_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Cancelled )
cls.Suspend ();
}

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?
 
senglory said:
using System;
using System.ComponentModel ;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;


namespace Retriever
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;


bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
bw.CancelAsync();
Thread.Sleep(5000);
}

static void bw_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Cancelled )
cls.Suspend ();
}

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?

Where is cls (in bw_RunWorkerCompleted()) declared? The code won't
compile for me.
 
[...]
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;
}
}
}
}
}
 
Back
Top