Multi-Threaded WinForm Application – BackgroundWorker

Welcome back to the second installment of a four part series (Part 1 – Basics, Part 3 – MVP and Part 4 – Dynamic Proxies).In Mult-Threaded WinForm Application – Basics, we discussed that long running operations render WinForm applications unresponsive. .NET 2.0 provides the BackgroundWorker component that allows us to perform long running operations in a background thread. The BackgroundWorker provides two interesting events DoWork and RunWorkerCompleted.DoWork is the event in which you should perform your long running operation. When calling the RunWorkerAsync method of BackgroundWorker the DoWork event is executed in a background thread.RunWorkerCompleted is the event in which you should update the GUI with the results from the work performed during the DoWork event.Example GUI using BackgroundWorker

void Button1Click(object sender, EventArgs e){    worker.RunWorkerAsync();}private string modelResults;void doWork(object sender, DoWorkEventArgs e){    modelResults = new Model().LongRunningMethod();}void workCompleted(object sender, RunWorkerCompletedEventArgs e){    label.Text = modelResults;}

If you run the above example, your GUI will remain responsive while the LongRunningMethod is running. You can test this by checking out the code at: checking out the code, compile and run the ThreadedView project. Clicking on the Perform Operation button. You can confirm that the GUI is still responsive by moving the window from side to side. The GUI continues to respond even though the LongRunningMethod is executing.Conclusion:BackgroundWorker is a good alternative for simple tasks, but I think it has some issues.

  1. I think that BackgroundWorker encourages users to put more code in the view than I am comfortable with doing.
  2. Harder to test (see #1 and #3).
  3. The use of threads is embedded into application. If you decide to use BackgroundWorker, you are tied to using threads. If you want to remove threads from the application, you have to do major surgery.

In my next post, I will discuss how to the Model-View-Presenter (MVP) to inject threads into your WinForm applications.If you have any questions or comments, please feel free to drop me a note.


4 Responses

  1. […] that does not use threads. This will allow us to revisit this application through out the series (Part 2 – BackgroundWorker) discussing different ways to inject threads into your WinForms […]

  2. […] application. In part one of this series, we discussed the basics of Multi-Threaded WinForms. In part two, we discussed using a BackgroundWorker to keep the WinForm application responsive during long […]

  3. […] the same material I presented on my blog on the same subject. You can read more about the basics, background worker and the MVP approaches in my previous posts. The code reviewed in the talk can be checked out from […]

  4. […] Multi-Threaded WinForms – Background Worker […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: