Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BackgroundWorker does not fire the RunWorkerCompleted event

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;             

    }
like image 893
Arcturus Avatar asked Jul 13 '11 09:07

Arcturus


People also ask

What is use of BackgroundWorker in C#?

BackgroundWorker is the class in System. ComponentModel which is used when you need to do some task on the back-end or in different thread while keeping the UI available to users (not freezing the user) and at the same time, reporting the progress of the same.

How does BackgroundWorker work in VB net?

BackgroundWorker handles long-running tasks. It does not freeze the entire program as this task executes. The BackgroundWorker type provides an excellent solution. It enables a simple multithreaded architecture for VB.NET programs.

What is BackgroundWorker in Winforms?

The BackgroundWorker class exposes the DoWork event, to which your worker thread is attached through a DoWork event handler. The DoWork event handler takes a DoWorkEventArgs parameter, which has an Argument property.

Is BackgroundWorker threaded?

BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.


1 Answers

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();  
like image 150
Henk Holterman Avatar answered Sep 23 '22 02:09

Henk Holterman