Calling BackgroundWorker synchronously

孤街浪徒 提交于 2020-01-22 14:53:32

问题


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 ?


回答1:


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



回答2:


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

}



回答3:


Your code that would be after //wait for execution to end should be placed in the worker_RunWorkerCompleted method.



来源:https://stackoverflow.com/questions/12213650/calling-backgroundworker-synchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!