6

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 ?

15
  • 6
    What's the point of using a BackgroundWorker if you synchronously wait for it? Aug 31, 2012 at 10:38
  • 1
    Why not just move the part of the code you want to execute after the BackgroundWorker is complete under RunWorkerCompleted event ?
    – coolmine
    Aug 31, 2012 at 10:39
  • 1
    @AhmedMasud: You should concentrate on fixing that error, not on building work arounds. Aug 31, 2012 at 10:42
  • 2
    @ChrisGessler But if you synchronously wait for the backgroundworker to end, you'll block the UI anyway Aug 31, 2012 at 10:45
  • 3
    @ChrisGessler "Asynchronous Programming with Async and Await", how is that synchronous? ;) Aug 31, 2012 at 10:48

3 Answers 3

19

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();
0
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
  • @GhostCat yes, you are right. I tried my best to answer the question through my post. Sep 8, 2017 at 13:22
  • @GhostCat thanks for your advice, I edited code and tried to present it more attractive. Sep 12, 2017 at 8:02
  • I would further "shorten" the empty method bodies - but much better now ;-)
    – GhostCat
    Sep 12, 2017 at 8:14
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.