Why the "RunWorkerCompleted" never comes after pressing ENTER?

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 Entar after running the bw_RunWorkerCompleted() will be never
invoked. Why?
 
F

Family Tree Mike

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 Entar after running the bw_RunWorkerCompleted() will be never
invoked. Why?

Please don't multipost. You can include multiple groups in a single
message so everyone sees the responses independent of the group they
first see it. I responded in microsoft.public.dotnet.framework with a
question regarding the declaration of the object cls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top