简体   繁体   English

同步调用BackgroundWorker

[英]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. 我希望执行代码在backgroundworker完成执行时结束。 My code for BackgroundWorker is here : 我的BackgroundWorker代码在这里:

{
    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 ... 如果您不希望代码异步执行,请不要将其放在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: 但是,如果您确实需要在BackgroundWorker中使用代码有一些模糊的原因,则可以使用以下命令:

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. 目标: BackgroundWorker应该同步执行。

Created an windows application form. 创建了一个Windows应用程序表单。 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. 单击button1时,它应该同步执行BackgroundWorker并返回参与UI,因此用户在完成BackgroundWorker任务之前无法执行任何操作。

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方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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