开发者

Nested BackgroundWorkers: RunWorkerCompleted calls on wrong thread?

I'm working on asynchronous operation which needs to invoke further asynchronous tasks. I'm trying to keep it simple by using BackgroundWorkers, with the result being that one BackgroundWorker's DoWork() callback calls a method which creates a second BackgroundWorker, like so (minus error checking and all that jazz for brevity):

开发者_Go百科
class Class1
{
    private BackgroundWorker _worker = null;

    public void DoSomethingAsync()
    {
        _worker = new BackgroundWorker();
        _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);
        _worker.DoWork += new DoWorkEventHandler(_worker_DoWork);
        _worker.RunWorkerAsync();
    }

    void _worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Class2 foo = new Class2();
        foo.DoSomethingElseAsync();
        while(foo.IsBusy) Thread.Sleep(0);  // try to wait for foo to finish.
    }

    void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // do stuff
    }
}

class Class2
{
    private BackgroundWorker _worker = null;
    Thread _originalThread = null;

    public AsyncCompletedEventHandler DoSomethingCompleted;

    public bool IsBusy { get { return _worker != null && _worker.IsBusy; } }

    public void DoSomethingElseAsync()
    {
        _originalThread = Thread.CurrentThread;

        _worker = new BackgroundWorker();
        _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);
        _worker.DoWork += new DoWorkEventHandler(_worker_DoWork);
        _worker.RunWorkerAsync();
    }

    void _worker_DoWork(object sender, DoWorkEventArgs e)
    {
        // do stuff
    }

    void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Debug.Assert(Thread.CurrentThread == _originalThread);  // fails

        // Assuming the above Assert() were excluded, the following event would be raised on the wrong thread.
        if (DoSomethingCompleted != null) DoSomethingCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, null));
    }
}

So the problem is, I'm expecting Class2._Worker_RunWorkerCompleted() to execute on the same thread on which Class2.DoSomethingElseAsync() was called. This never happens - instead, the callback runs on a completely new thread.

Here's my suspicion: Class1's _worker_DoWork() never returns, which means that thread would never get back to an event listener, even if one existed (I suspect one doesn't). On the other hand, if _worker_DoWork() did return, Class1's BackgroundWorker would automatically finish prematurely - it needs to wait for Class2 to finish working before it can finish its work.

That leads to two questions:

  1. Is my suspicion correct?
  2. What's the best way to nest asynchronous operations like this? Can I salvage the BackgroundWorker approach, or is there some other, more suitable technique?


If a BackgroundWorker is created on the UI thread, DoWork will run on a thread pool thread and RunWorkerCompleted will run on the UI thread.

If a BackgroundWorker is created on a background thread (ie not the UI thread) DoWork will still run on a thread pool thread and RunWorkerCompleted will also run on a thread pool thread.

In your case, since you can't marshal a call to an arbitrary (thread pool) thread, you won't be able to guarantee the behaviour you want, although you might want to take a look at System.Threading.SynchronizationContext.


You should use ManualResetEvent to communicate between threads:

http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28VS.71%29.aspx


Firstly, I can't see anywhere that actually starts running the worker. You could change the DoSomethingAsync method (also add the call to the DoSomethingElseAsync method in Class2)

public void DoSomethingAsync()
{
    _worker = new BackgroundWorker();
    _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);
    _worker.DoWork += new DoWorkEventHandler(_worker_DoWork);
    _worker.RunWorkerAsync(); // add this line to start it
}

Secondly, the work handler (the _worker_DoWork method) is not guaranteed to be on the same thread as the call to DoSomethingAsync - this is the whole point of the Background Worker. ie/ to do work on another thread. The same applies for worker complete handler (the _worker_RunWorkerCompleted method).

Finally, It doesn't seem to make sense to attach the two different background workers unless the top level one (Class1) always requires Class2 work to happen too. You would be better of having a single manager to handle each background worker.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜