Interrupting an Action in background worker DoWork.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
Is there any way to stop executing a method Foo() impedimently after
<pre class="prettyprint" style="font-size:12px worker.CancelAsync()[/code]
method is called? Here is my code:
<pre class="prettyprint class Program
{
static void Main(string[] args)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
Console.WriteLine(String.Format("--Starting bg worker"));
worker.RunWorkerAsync();
Console.WriteLine(String.Format("--Waiting 3 seconds"));
System.Threading.Thread.Sleep(3000);
Console.WriteLine(String.Format("--Killing a bgworker"));
worker.CancelAsync();
Console.ReadLine();
}

static void worker_DoWork(object sender, DoWorkEventArgs e)
{
Action action = new Action(() =>
{
Foo();
});

action();
}

static void Foo()
{
for (int i = 0; i < 50; i++)
{
Console.WriteLine(String.Format("I is equal to: {0}...", i));
System.Threading.Thread.Sleep(500);
}
}

}[/code]

Thanks in advance,
panqnik


View the full article
 
Back
Top