|
-
August 4th, 2009, 07:36 PM
#1
Why the "RunWorkerCompleted" never comes after pressing ENTER?
Code:
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?
Last edited by senglory; August 4th, 2009 at 07:37 PM.
Reason: BackgroundWorker RunWorkerCompleted CancelAsync
-
August 4th, 2009, 10:13 PM
#2
Re: Why the "RunWorkerCompleted" never comes after pressing ENTER?
the code does not compile:
cls.Suspend (); <--- cls does not exist in the current context
You may be running a previously compiled version; not the logic you think
-
August 5th, 2009, 10:18 AM
#3
Re: Why the "RunWorkerCompleted" never comes after pressing ENTER?
Code:
...
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
namespace Retriever {
class Program {
static BackgroundWorker bw = new BackgroundWorker();
static void Main(string[] args) {
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(); // This only assigns that the thread should be cancelled !!
Thread.Sleep(5000);
}
static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (e.Cancelled)
Console.WriteLine("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 (bw.CancellationPending) {
e.Result = "done";
e.Cancel = true;
return;
}
}
}
}
}
Your backgroundprocess will be assigned to be cancelled but isn't really cancelled yet ( The cancellation is pending until its acknowledged in the loop) , so you will not get workerComleted fired. Use it as I showed and it will do the job
Last edited by JonnyPoet; August 5th, 2009 at 10:30 AM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|