logo
  • Jobs
  • About Me
  • Contact
  • Home
« Generic ShowDialog Command
Goodbye, Acrobat Reader »

BackgroundWorker component

Posted March 4th, 2006 by Matt Berther

More and more, Im spending time in .NET 2.0 and more and more Im finding *really* cool things. The latest one I found was the BackgroundWorker component. This is probably old hat to most of you, but I was really impressed to see this. From the MSDN documentation:

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

To execute a time-consuming operation in the background, create a BackgroundWorker and listen for events that report the progress of your operation and signal when your operation is finished. You can create the BackgroundWorker programmatically or you can drag it onto your form from the Components tab of the Toolbox. If you create the BackgroundWorker in the Windows Forms Designer, it will appear in the Component Tray, and its properties will be displayed in the Properties window.

To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the operation, call RunWorkerAsync. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.

Instantiating code on a background thread is as simple as this:

private void button1_Click(object sender, EventArgs e)
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    worker.RunWorkerAsync();
}
 
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // This is the method that gets executed on a background thread.
    // Since you should not update the UI from a thread, make sure
    // you use the RunWorkerCompleted handler if you need to do that.
    // You can assign e.Result to a value which can be used in that handler.
    SomeLongRunningWebServiceCall();
}
 
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // this is back on the main UI thread, so you can update the UI
    // e.Result is assigned to whatever you've assigned it to in the
    // DoWork handler
}

How cool and easy is that?

This entry was posted on Saturday, March 4th, 2006 at 2:26 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Ayende Rahien
March 5th, 2006

On button1_Click(), wouldn’t calling dispose pretty much guarantee that the work will be aborted?

Matt Berther
March 5th, 2006

Of course, youre right. Ive updated the sample.

.Net Adventures
March 12th, 2006

At this url (http://idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11) you can find similar component for Net 1.1

Jalil Vaidya
March 20th, 2006

The worker variable needs to be declared at form level. It is local to click button1_Click function and is candidate for garbage collection any time after RunWorkerAsync is called. I understand this is just a demo code to introduce this new class to your readers :-)

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
Jobs
mattberther.com © 2003 - 2009