BackgroundWorker method executing twice
-
Below is the skeleton code which demonstrates the way I have used the BackgroundWorker class. I followed the example provided in MSDN. For some reason MyTimeConsumingTask executes twice before the bgWorker_RunWorkerCompleted handles it. Any help would be greatly appriciated.
//Form Class void bgWorker\_DoWork(object sender, DoWorkEventArgs e) { // Get the BackgroundWorker that raised this event. BackgroundWorker worker = sender as BackgroundWorker; e.Result = MyTimeConsumingTask(e.Argument, worker, e); } void bgWorker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.Error != null) { MessageBox.Show(e.Error.Message); } else if ((bool)e.Result) { //MyTimeConsumingTask successfully executed } else { //MyTimeConsumingTask failed } } //Form Class end //Another Class internal bool MyTimeConsumingTask(object arg, BackgroundWorker worker, DoWorkEventArgs e) { //Actual task is performed here, for simplicity sleep has been used. Thread.Sleep(10000); return true; } //Another Class end
-
Below is the skeleton code which demonstrates the way I have used the BackgroundWorker class. I followed the example provided in MSDN. For some reason MyTimeConsumingTask executes twice before the bgWorker_RunWorkerCompleted handles it. Any help would be greatly appriciated.
//Form Class void bgWorker\_DoWork(object sender, DoWorkEventArgs e) { // Get the BackgroundWorker that raised this event. BackgroundWorker worker = sender as BackgroundWorker; e.Result = MyTimeConsumingTask(e.Argument, worker, e); } void bgWorker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.Error != null) { MessageBox.Show(e.Error.Message); } else if ((bool)e.Result) { //MyTimeConsumingTask successfully executed } else { //MyTimeConsumingTask failed } } //Form Class end //Another Class internal bool MyTimeConsumingTask(object arg, BackgroundWorker worker, DoWorkEventArgs e) { //Actual task is performed here, for simplicity sleep has been used. Thread.Sleep(10000); return true; } //Another Class end
Put break points and step into the code to see what is wrong.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Below is the skeleton code which demonstrates the way I have used the BackgroundWorker class. I followed the example provided in MSDN. For some reason MyTimeConsumingTask executes twice before the bgWorker_RunWorkerCompleted handles it. Any help would be greatly appriciated.
//Form Class void bgWorker\_DoWork(object sender, DoWorkEventArgs e) { // Get the BackgroundWorker that raised this event. BackgroundWorker worker = sender as BackgroundWorker; e.Result = MyTimeConsumingTask(e.Argument, worker, e); } void bgWorker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.Error != null) { MessageBox.Show(e.Error.Message); } else if ((bool)e.Result) { //MyTimeConsumingTask successfully executed } else { //MyTimeConsumingTask failed } } //Form Class end //Another Class internal bool MyTimeConsumingTask(object arg, BackgroundWorker worker, DoWorkEventArgs e) { //Actual task is performed here, for simplicity sleep has been used. Thread.Sleep(10000); return true; } //Another Class end
You're not accidentally adding another handler to DoWork are you? bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); If you do that, it will be called twice.
- S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!
-
Put break points and step into the code to see what is wrong.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Checked with break points at every line of code involving this event. Although the e.result of first execution is true, the bgWorker_RunWorkerCompleted doesn't handle it but handles it after second execution. Below is the sequence of the code execution as traced by break points, strange but true...:confused:
BackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handlerThread.Sleep(10000); //MyTimeConsumingTask method
return true; //MyTimeConsumingTask mehtodBackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handlerThread.Sleep(10000); //MyTimeConsumingTask method
return true; //MyTimeConsumingTask methodelse if ((bool)e.Result) //bgWorker_RunWorkerCompleted handler
-
You're not accidentally adding another handler to DoWork are you? bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); If you do that, it will be called twice.
- S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!
-
Checked with break points at every line of code involving this event. Although the e.result of first execution is true, the bgWorker_RunWorkerCompleted doesn't handle it but handles it after second execution. Below is the sequence of the code execution as traced by break points, strange but true...:confused:
BackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handlerThread.Sleep(10000); //MyTimeConsumingTask method
return true; //MyTimeConsumingTask mehtodBackgroundWorker worker = sender as BackgroundWorker; //bgWorker_DoWork handler
e.Result = MyTimeConsumingTask(e.Argument, worker, e); //bgWorker_DoWork handlerThread.Sleep(10000); //MyTimeConsumingTask method
return true; //MyTimeConsumingTask methodelse if ((bool)e.Result) //bgWorker_RunWorkerCompleted handler
Try using trace statements instead of watching it through the debugger, and put a breakpoint at the end of the OnCompleted method. When the breakpoint is hit, examine the trace statements.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001