Consider how many times we’ve run into the following scenario: We’ve got a GUI design, say, a simple WinForm. Upon interaction with the form, we need to launch a CPU-intensive operation, one which might take a while. At some point the operation will complete and we want the result to show on the form. That’s where the problem starts...
We’ve all seen this before – the GUI becomes unresponsive while the operation takes place. This is because we are violating the following rule:
“Never execute a long-running piece of code in the UI thread”
as stated here by Jon Skeet:
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml
Now, I know we can launch the operation on a separate thread using the following:
Thread t = new Thread(new ThreadStart(Foo));
t.Start();
where Foo() would carry out the dirty work for us. The problem is really when we want to update the GUI with the result of the operation. We get the dreaded “Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on.” This might lead us to all the BeginInvoke and EndInvoke stuff to handle the cross-thread updates in a safe manner – not a pretty solution but it does work.
I had heard a while back about the BackgroundWorker class but just had never tried it out. I wanted to test it on a small application before attempting it on any Falafel production software - I’ll be wrecking their code plenty of times without having to do it deliberately here! So I created a simple app:

I needed an intensive calculation to happen when I clicked the Go button, so I found this:
http://en.wikipedia.org/wiki/Sieve_of_Erastothenes
It provides an interesting algorithm for finding all the prime numbers up to a Maximum Count. I found an excellent implementation of this algorithm by Mariano Abdala at the Code Project:
http://www.codeproject.com/KB/recipes/highspeed_primenumbers.aspx
I created a simple class called Busy with a single function:
public int FindTotalPrimesBelow(int max)
{
// Prime number algorithm here
}
I won’t distract you with guts of the algorithm just yet. The function finds all the primes below the max value and then returns the total number of primes found. Now, in the form class, handle the button click:
private void btnGo_Click(object sender, EventArgs e)
{
btnGo.Enabled = false;
total = busy.FindTotalPrimesBelow(Convert.ToInt32(tBMax.Text));
tBTotal.Text = total.ToString();
btnGo.Enabled = true;
}
Seems simple enough, right? Just enter a suitably large number for the Maximum Count and click the Go button. You might want to adjust the Count up or down depending on the speed of your machine. After several seconds, the result shows up:

If you watch the Task Manager, you will see the CPU rail for the duration of the operation. If you try to interact with the form during the calculation, you’ll get the lovely “Not Responding” message in the title bar as well as in the Windows task bar at the bottom.
Time for a little multithreading here, eh? Perhaps in a more elegant way than all that Invoke stuff? Enter the BackgroundWorker class...stay tuned.