Thread Marshalling Part 2 – using BackgroundWorker
- Part 1 – Creating a thread in Windows Forms - Some basic ideas about firing a background and how to synchronize it with the UI.
- Part 2 – Using BackgroundWorker - the .NET 2.0 way using a BackgroundWorker component
- Part 3 – Automatic Marshalling – creating your own component with auto marshalling
The pattern described in the prevoius post is so common, that .NET 2.0 introduced a new tool to make it easier: BackgroundWorker. Let’s rewrite the code of our form to take advantage of this component:
1 public partial class Main : Form
3 private readonly EmptyLoop _emptyLoop = new EmptyLoop(1000000000);
4 private readonly BackgroundWorker _bgWorker = new BackgroundWorker();
6 public Main()
11 private void bStart_Click(object sender, EventArgs e)
13 progressBar.Visible = true;
14 bCancel.Enabled = true;
16 _emptyLoop.UpdateProgress += algorithm_UpdateProgress;
18 _bgWorker.WorkerReportsProgress = true;
19 _bgWorker.WorkerSupportsCancellation = true;
20 _bgWorker.DoWork += (worker, eventArgs) => _emptyLoop.Go();
21 _bgWorker.ProgressChanged += (worker, eventArgs) =>
23 _bgWorker.RunWorkerCompleted += (worker, eventArgs) =>
28 private void ShowProgress(int percent)
30 progressBar.Value = percent;
33 private void ShowAlgorithmCompleted()
35 progressBar.Value = 0;
36 progressBar.Visible = false;
37 bCancel.Enabled = false;
40 private void algorithm_UpdateProgress(object sender,
41 EmptyLoop.UpdateProgressEventArgs e)
43 if (!_bgWorker.CancellationPending)
53 private void bCancel_Click(object sender, EventArgs e)
Looks familiar but there is number of significant differences from prevoius implementation:
- The functionality provided by BackgroundWorker class is quite similar to the design of EmptyLoop class. This is not by accident, operations like: reporting progress, cancelling and completing are in fact common to asynchronous operations. We still need to send a message about the progress report from our EmptyLoop and as we would like to keep its implementation clean from any unnecessary dependencies, events are the best way.
- Canceling operation is quite different. Cancel button calls BackgroundWorker CancelAsync method, which sets CancellationPending flag to true. Normally DoWork event handler should check this flag and cancel working. In our case we do not have a way to pass the canceling message from DoWork handler, instead we check the cancelling flag in report progress method. This will stop the algorithm a little bit later, but still works.
- Finally: thread marshalling method calls: Invoke or BeginInvoke are gone! This is because BackgroundWorker marshals event calls automatically.
The last information is quite interesting, especially if you take into account that the BackgroundWorker lives in System.ComponentModel namespace and is supposed to be a generic asynchronous component – it can be used in WinForms, WPF and any environment you like. Somehow it knows how to do things like thread marshalling.
If we take a look into its RunWorkerAsync method, we can see that it takes advantage of the AsyncOperationManager static class. This static class has one property SynchronizationContext and one method CreateOperation. This members allow the hosting environment (like Windows Forms) set its SynchronizationContext - the object which describes the behaviour required for asynchronous operations. What is interesting, as this an extensible model, we can try to use this mechanism for our purposes.