Creating a better BackgroundWorker: CancelImmediately and other goodies

This blog has moved to


  • I'm still stuck in 1.1 - so: from what you could tell, is the final BackgroundWorker component any different from the one Juval created?

    One of the challenges involved with using the BWC is the non-typed nature of the e.Argument. I ended up writing a CodeSmith template to bundle my method parameters into a class to simplify the process...

  • Oskar: The final component is not exactly the same but it works the same as the one in 1.1. The one Juval wrote was based in design on the final one that was released in 2005.

    The final one employs .NET 2.0 specific classes such as AsyncOperation to do its bidding which was not possible in 1.1.

    They could have used generics to help with the untyped natire of the e argument, but they somehow chose not to...

  • But the interface is the same? Method/Event names, etc?

  • Do you have any unit testing examples working with these classes?

  • I think I am misunderstanding something. :-)

    I was hoping the BackgroundWorkerEx.StopImmediately would stop immediately, but it doesn't. This code always fails:

    if (bwEx.IsBusy) {
    Debug.WriteLine("Calling StopImmediately()");
    Debug.WriteLine("StopImmediately() returned");

    Despite getting both messages in the immediate window, I still get a InvalidOperationException("BackgroundWorker_WorkerAlreadyRunning") when I call bwEx.RunWorkerAsync(...).

    Do I misunderstand what this component is for or how to use it?


  • Calling them immediately one after the other may produce this problem due to a resource locking problem.
    Calling thread.sleep(1) inside the "RunWorkerAsync" method as the first line shoudl solve it..

Comments have been disabled for this content.