The BackgroundMultiWorker is modelled on the BackgroundWorker but provides support for multiple background operations. Each task is represented by a token, which can be any object at all. Just note that two value type objects with the same value will be recognised as the same token. As a result, using numbers or strings as tokens is not recommended if they may be duplicated. Instead, create a reference type object with a single property of the desired type.
The members of the BackgroundMultiWorker closely resemble those of the BackgroundWorker class, except that most either require or provide a token to identify the current task. For instance, when calling RunWorkerAsync to start a new operation, you pass in a token to identify that operation. When the DoWork event is raised, that token is returned via the Token property of the DoWorkEventArgs object received by the event handler. This allows you to determine which task you're doing the work for.
The CancellationPending and IsBusy properties of the BackgroundWorker have been translated to the IsCancellationPending and IsBusy methods of the BackgroundMultiWorker. Both are overloaded to allow you to determine the overall state or the state for a particular task, identified by a token.
Finally, ReinstateCancelledOperation and ReinstateCancelledOperations methods have been added, allowing you to "uncancel" a cancelled task.
I haven't actually tested the code yet but I thought I'd post it anyway. I'll get to testing it myself soon everyone is welcome to view the code and put it to use if they're happy to fix any issues that arise themselves.
Note that the solution was created in VS 2010 targeting .NET 2.0. If you're using an earlier version of VS, you should be able to just create your own Class Library project and import the code files. In that case, you will need to reference System.Drawing.dll for the ToolboxBitmap attribute.
UPDATES:
1.0.0.1: Added DefaultEvent attribute and test application.
Last edited by jmcilhinney; Mar 17th, 2011 at 01:00 AM.