Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling BackgroundWorker synchronously

I want to call the background worker synchronously. I want execution of the code to end when backgroundworker has completed its execution. My code for BackgroundWorker is here :

{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += DoWork;
    worker.RunWorkerCompleted += RunWorkerCompleted;
    ...
    worker.RunWorkerAsync();
    //wait for execution to end 
}

One way of doing it will be to check the status again n again until its execution is completed but is there any other good way of doing it ?

like image 423
Ahmed Masud Avatar asked Aug 31 '12 10:08

Ahmed Masud


People also ask

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.

What is use of BackgroundWorker in C#?

BackgroundWorker makes the implementation of threads in Windows Forms. Intensive tasks need to be done on another thread so the UI does not freeze. It is necessary to post messages and update the user interface when the task is done.

What does RunWorkerAsync Do?

The RunWorkerAsync method submits a request to start the operation running asynchronously. When the request is serviced, the DoWork event is raised, which in turn starts execution of your background operation. If your operation requires a parameter, you can provide it as the argument parameter to RunWorkerAsync.

What is BackgroundWorker?

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running.


2 Answers

If you don't want your code to execute asynchronously, don't put it in a BackgroundWorker...

{ 
    DoWork();
} 

However, if there is some obscure reason why you absolutely need to have the code in the BackgroundWorker, you can use the following:

ManualResetEvent mre = new ManualResetEvent(false);
BackgroundWorker worker = new BackgroundWorker(); 
worker.DoWork += DoWork; 
worker.RunWorkerCompleted += (s, e) => 
                             {
                                 RunWorkerCompleted(s, e); 
                                 mre.Set();
                             };
// ... 
worker.RunWorkerAsync();
mre.WaitOne();
like image 170
Daniel Hilgarth Avatar answered Sep 24 '22 15:09

Daniel Hilgarth


Objective: BackgroundWorker should execute in sync.

Created an windows application form. On click of button1 it should execute BackgroundWorker synchronously and returns engage the UI, So user not able to do anything till completion of BackgroundWorker task.

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
        InitializeComponent(); 
    }

    BGimplent obj = null;

    private void button1_Click(object sender, EventArgs e)
    {
        int i = 0;
         obj = new BGimplent();
        obj.eveBG += obj_eveBG;
        i = 5;
        obj.MyProperty = 5;
        obj.DoConfig();
        obj.ManualReset.WaitOne();

        obj.MyProperty = 10;
        obj.MyProperty = 11;
        obj.MyProperty = 12;
        obj.MyProperty = 13;

        obj.MyProperty = 14;
    }

    void obj_eveBG(string s)
    {
        obj.ManualReset.Set();
        MessageBox.Show(s);
    }
}



/*
*******************************************************
    Paste below code in adding new class i.e. Class1


*/
public delegate void delBG(string s);

class BGimplent
{
    public event  delBG eveBG;


    private ManualResetEvent mnuReset = new ManualResetEvent(false);
    public ManualResetEvent ManualReset { get; set; }

    public int MyProperty { get; set; }

    BackgroundWorker bgWorker = new BackgroundWorker();
    public void DoConfig()
    {
        ManualReset = mnuReset;

        bgWorker.DoWork += bgWorker_DoWork;
        bgWorker.ProgressChanged += bgWorker_ProgressChanged;
        bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
        bgWorker.RunWorkerAsync();            
    }

    void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {   
        Thread.Sleep(5000);
        if (eveBG != null)
            eveBG("Value of MyProperty: " + MyProperty.ToString());
    }

}
like image 29
3 revs Avatar answered Sep 20 '22 15:09

3 revs