BackgroundWorker objects are an addition to .NET 2.0 to simplify asynchronous programming and are very useful in (but not restricted to) Windows Forms.
The basic idea is:
- Create a BackgroundWorker object
- Set-up various events on it, like DoWork, RunWorkerCompleted, and ProgressChanged
- Set-up various properties, like WorkerReportsProgress and WorkerSupportCancellation
- Call RunWorkerAysnc
…then, in your form you get notified of progress and can cancel the operation at any time.
I’ve created a ReSharper Live Template that will help you create and set-up the BackgroundWorker object. You can download it here. For instructions on importing the Live Template into ReSharper, see my previous post here.