BackgroundWorker does not fire the RunWorkerCompleted event
The completed event is Invoked to the main thread. It is supposed to be picked up and executed by the MessagePump.
However, your Wait-and-Sleep code is blocking the message loop.
Hide();
....
while (!BusinessLogic.firstCycleDone)
{
Thread.Sleep(100);
}
Show();
The answer here is that you have no use for a Backgroundworker or another form of threading...
Just call bgWorker_DoWork()
directly:
// Hide();
bgWorker_DoWork(); // rename
Show();
Arcturus
Updated on June 04, 2022Comments
-
Arcturus almost 2 years
I'm creating backgroundworker not in my windows form but in the class file (BusinessLogic) that implements all the processing. From main form I first call the BL method that initializes the BGW. Then I call the method of BL which will start the BGW.
Here is more background :) on my implementation. How to use BackGroundWorker in class file?
The DoWork event runs fine but it doesnt call the RunWorkerCompleted.
Some googling and I found out this link. I've a feeling that my problem is same as this guys. http://www.eggheadcafe.com/software/aspnet/29191764/backgroundworker-does-not-fire-the-runworkercompleted-event.aspx
I'd appreciate any input on this issue. Thanks in advance.
Code in Main form:
private void frmMain_Load(object sender, EventArgs e) { Hide(); BusinessLogic.BGWInitialize(); BusinessLogic.StartBackgroundWorker(); while (!BusinessLogic.firstCycleDone) { Thread.Sleep(100); } Show(); }
Code in BusinessLogic:
public static void BGWInitialize() { bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged); bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted); bgWorker.WorkerReportsProgress = true; } public static void StartBackgroundWorker() { bgWorker.RunWorkerAsync(); } private static void bgWorker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { firstCycleDone = true; }
-
ZombieSheep almost 13 years(Slightly off-topic) I have to assume that this isn't the code you're going to be putting live, but just in case it is, why are you calling out to a new BG thread to do the work, but then invoking Thread.Sleep() on the calling thread? Why not just do the work synchronously?
-
Arcturus almost 13 yearsumm yes I just discovered that. :) I'm doing this (calling BGW) across multiple forms in my app and implemeted it in the main form by default. However main form doesnt need BGW since i want the app to open only after the processing is done.
-
-
Jason Goemaat almost 13 yearsOr if you do want to block the main thread for some reason, set the flag before your do work routine exits.
-
Arcturus almost 13 yearsThanks that solved the issue. And yes that was pretty dumb of me to use threading and then halt main thread till the worker thread completed the processing. In my defence I'm using BGW in multiple forms across my app and this is the only instance where I'd need to wait for processing to complete. So I just copy pasted my other code here and didn't think that it wouldn't be needed in this case.
-
Henk Holterman almost 13 yearsThe backgroundworker is not buggy. But most programs that use Application.DoEvents() are.
-
Djole almost 13 yearsI have not said that it is a perfect solution, and in my experience
BackgroundWorker
is buggy. Can I have my own opinion ? -
Henk Holterman almost 13 yearsOnly if you back it up (examples/links)
-
Omtara over 11 years@HenkHolterman My main thread is waiting on a semaphore that the RunWorkerCompleted is supposed to Release and this is causing a deadlock. How could I wait on the main thread while ensuring that the RunWorkerCompleted could be called?
-
Henk Holterman over 11 years@Omtara - make that a separate question (after looking for duplicates)