简体   繁体   中英

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 ?

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();

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());
    }

}

//wait for execution to end后的代码应放在worker_RunWorkerCompleted方法中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM