Article Options
Premium Sponsor
Premium Sponsor

Multithreading The Easy Way: The BackgroundWorker
by Ged Mead | Published  06/01/2008 | Framework 2.0 Framework 3.0 .NET Intermediate Visual Studio 2005 Visual Studio 2008 Visual Basic 2005 Windows Presentation Foundation | Rating:
Ged Mead

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.

 

View all articles by Ged Mead...
Summary

  This demonstration has used a very simple background task as its example, but you can use the same approach for more complex tasks.

   The BackgroundWorker makes it very easy to keep your application flowing while a time consuming, possibly CPU intensive, task is running in the background.  With the ReportProgress method you can give your users feedback as to what percentage of the task has completed (and optionally could of course use this value as a straightforward numeric value), together with visual notification, usually in the form of a text message .

  This very useful component shields you from the more technical intricacies and potential difficulties of using multithreading in your projects.  (However, if you do want more technical detail of multithreading, check out John Spano's article which you can read here.)

   I hope you'll find this introduction useful and will be able to use the techniques shown in projects of your own in the future.

  A sample application created using the Express Edition of VB 2008 is attached.  (If you prefer to use VB 2005 Express Edition or a full version of Visual Studio 2005, you can create a new project in VB 2005 and import the code files using the "Add Existing Item" menu choice.)

 

 

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.75000000000001 out of 5
 20 people have rated this page
Article Score5393
Attachments
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 06/04/2008)
Rating
Very clear and easy to follow. AND the sample Worked!!!!
 
Comment #2  (Posted by RAB on 06/12/2008)
Rating
Can we use teh Backgroundworker component without a form? Can it be instantiated in a module?
 
Comment #3  (Posted by Ged Mead on 06/20/2008)
Rating
Yes you can do that. However as there is no toolbox available for a Module you have to create the bgw in code. To do this you use Dim bgw as New System.ComponentModel.BackgroundWorker.
You will of course also need to set the properties and add handlers in code.
If you need more info on this question please post a question in the vbcity VB.NET forums (where it's easier for me to post code samples) :-}
 
Comment #4  (Posted by an unknown user on 07/30/2008)
Rating
Thank a lot XTab
 
Comment #5  (Posted by an unknown user on 08/08/2008)
Rating
Great step-by-step with in-line code. Ged obviously put alot of time in this well written article.
More please !
 
Comment #6  (Posted by an unknown user on 08/13/2008)
Rating
I try to read everything you publish. Great stuff.
 
Comment #7  (Posted by an unknown user on 08/18/2008)
Rating
Excellent and simple to use.
 
Comment #8  (Posted by an unknown user on 09/06/2008)
Rating
practical, readable
 
Comment #9  (Posted by an unknown user on 09/29/2008)
Rating
This guide was one of the best guides I've read!

You just helped me understand something I origionally had no clue about. Thanks!
 
Comment #10  (Posted by Matt Higginbotham on 10/31/2008)
Rating
Thanks Ged! What a great article.. This helped me a bunch with some long database queries that i have.

Keep Em' Coming
 
Comment #11  (Posted by an unknown user on 11/03/2008)
Rating
Very good except the cancelation did not work correctly, although it cancelled the task if I cancelled it and then re-ran I got "Operation has already had OperationCanceled called on it". I need to reset the canelled property at finish but, its read only any ideas?
 
Sponsored Links