Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wait correctly until BackgroundWorker completes?
    primarykey
    data
    text
    <p>Observe the following piece of code:</p> <pre><code>var handler = GetTheRightHandler(); var bw = new BackgroundWorker(); bw.RunWorkerCompleted += OnAsyncOperationCompleted; bw.DoWork += OnDoWorkLoadChildren; bw.RunWorkerAsync(handler); </code></pre> <p>Now suppose I want to wait until <code>bw</code> finishes working. What is the right way to do so?</p> <p>My solution is this:</p> <pre><code>bool finished = false; var handler = GetTheRightHandler(); var bw = new BackgroundWorker(); bw.RunWorkerCompleted += (sender, args) =&gt; { OnAsyncOperationCompleted(sender, args); finished = true; }); bw.DoWork += OnDoWorkLoadChildren; bw.RunWorkerAsync(handler); int timeout = N; while (!finished &amp;&amp; timeout &gt; 0) { Thread.Sleep(1000); --timeout; } if (!finished) { throw new TimedoutException("bla bla bla"); } </code></pre> <p>But I do not like it.</p> <p>I have considered replacing the <code>finished</code> flag with a synchronization event, set it in the <code>RunWorkerCompleted</code> handler and block on it later instead of doing the while-sleep loop. </p> <p>Alas, it is wrong, because the code may run in the WPF or WindowsForm synchronization context, in which case I would block the same thread as the <code>RunWorkerCompleted</code> handler runs on, which is clearly not very smart move.</p> <p>I would like to know of a better solution.</p> <p>Thanks.</p> <p><strong>EDIT:</strong></p> <p>P.S.</p> <ul> <li>The sample code is so contrived intentionally to clarify my question. I am perfectly aware of the completion callback and yet I want to know how to wait till completion. That is my question.</li> <li>I am aware of <code>Thread.Join</code>, <code>Delegate.BeginInvoke</code>, <code>ThreadPool.QueueUserWorkItem</code>, etc... The question is specifically about <code>BackgroundWorker</code>.</li> </ul> <p><strong>EDIT 2:</strong></p> <p>OK, I guess it will be much easier if I explain the scenario. </p> <p>I have a unit test method, which invokes some asynchronous code, which in turn ultimately engages a <code>BackgroundWorker</code> to which I am able to pass a completion handler. All the code is mine, so I can change the implementation if I wish to. I am not going, however, to replace the <code>BackgroundWorker</code>, because it automatically uses the right synchronization context, so that when the code is invoked on a UI thread the completion callback is invoked on the same UI thread, which is very good.</p> <p>Anyway, it is possible that the unit test method hits the end before the BW finishes its work, which is not good. So I wish to wait until the BW completes and would like to know the best way for it.</p> <p>There are more pieces to it, but the overall picture is more or less like I have just described.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload