AvocadoSoftware.com

Software For Hardcore Developers
Welcome to AvocadoSoftware.com Sign in | Join | Help
in Search

Derick Baileys old blog archives - go to derickbailey.com for new contents

VS 2005 Cool Feature No. 6: Background Worker Component

Background Worker Component

I've often created a Winforms application that required a lot of system resources to process, and wanted to allow the forms of the application to continue functioning, while this process was running. The most common example of this, is a simple progress bar to indicate how far along the process is. In previous versions of Visual Studio, I've always created thread to run this process and provided various events from whatever objects was being run. Now, with Visual Studio 2005 and the .NET Framework 2.0, Microsoft has provided a simple component to solve this common problem: the Background Worker component.

Simple drag & drop the component from the Toolbox onto your Winforms form

and in your code file, you will have access to three events provided by this component

Each of these events provides various information on what is happening with the background worker. From the MSDN Library:

 

  Name Description
Public event DoWork Occurs when RunWorkerAsync is called.
Public event ProgressChanged Occurs when ReportProgress is called.
Public event RunWorkerCompleted Occurs when the background operation has completed, has been canceled, or has raised an exception.

To start up a background process, all you need to do is call the BackgroundWorker.RunWorkerAsync() method. This fires off the DoWork event inside of the context of a new thread, allowing your code to execute in the background. Inside of the DoWork event, you can grab a reference to the BackgroundWorker that made the call, by CType-ing the Sender parameter. From there, you can call the various other methods of the BackgroundWorker object, to provide progress indication, notification of completion, etc. Microsoft has provided some nice sample code in the MSDN article, How to: Run an Operation in the Background.

 

Published Saturday, October 29, 2005 5:29 PM by dredge
Filed Under: , ,
New Comments to this post are disabled

This Blog

Post Calendar

<October 2005>
SuMoTuWeThFrSa
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

Advertisement

News

this is my old blog archives - go to http://derickbailey.com for updates

Syndication

Advertisement

Powered by Community Server, by Telligent Systems