Click here to Skip to main content
13,349,997 members (49,814 online)
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 20 Jul 2006

Asynchronous Invocation Using BackgroundWorker

, 20 Jul 2006
Rate this:
Please Sign up or sign in to vote.
A simple article on implementing asynchronous code, in C#.

Sample Image


When you have a function that takes a long time and it freezes the screen, the user gets nervous. They simply don't like to lose the control. It would give a friendly impression when they can still click on the form while the application is busy with getting the information, for example. When you start implementing asynchronous methods, you will wonder which technique is best suitable for your application. Would you use a Timer class or a Thread class or a BackgroundWorker? This article uses a BackgroundWorker class to show how easy it is, and where you need to pay attention when using it.


I have tried to compare different methods of implementing Asynchronous Invocation in my weblog. With Microsoft Visual Studio 2005, I discovered this class which is new to me.

Using the code

I have put extra comments everywhere possible, to make the sample a snippet of code you can use to start your own implementation. The code implements a very simple action, which is finding files in a directory to simulate the time consuming action. This is the place where you need to change. For the rest, the skeleton should be kept the same.

The background worker is able to send an event to the called thread to change the screen appearance. That is very easy, and I thought it would be easy for everyone to discover. But what if that is not enough and you would like to send other messages back to the caller? So I used BeginInvoke to invoke a call on the caller thread.

I have two delegates used from the sub thread, to call the method implemented in the main thread.

private delegate void PaintMessage(TreeNode root, 
                                   string childText);
private PaintMessage myAddNode = null;

The OnPaontMessage implements the adding of a newly found file into an existing (given) root. This is called when the sub-thread finds a new item.

private delegate void EnableCancel(bool enabled);
private EnableCancel myEnableCancel = null;

The second delegate is to indicate that the thread has been started and that it could be stopped. It is implemented by the main thread to enable the Cancel button.

Starting the action will be done by calling the RunWorkerAsync method on the worker.


When a sub-folder is found, I send a notification to the main thread to create a node for the sub-folder. Then, I use that node to create a file node in it. But the dilemma is that I can not proceed with filling the file, if the main thread didn't get the chance to create the sub folder. Thus, I need to wait and let the main application take the chance to create it. One simple way to do this is the following loop:

// Create a folder node before proceeding
IAsyncResult ar = BeginInvoke(myAddNode, new object[]
                  { root, di.Name });
// Update the screen

// Wait until the folder has been created before
// proceeding with the content of the folde
while (!ar.IsCompleted)
    ar.AsyncWaitHandle.WaitOne(-1, false);

First, I invoke the delegate and I keep the reference to it. I use the reference to see if it has been completed. And in the loop, I let the application take the chance and do its events. Meanwhile, I tell my sub-thread to wait a bit.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Asghar Panahy
Systems Engineer
Netherlands Netherlands
I am a Microsoft certified application developer and work on Microsoft platform since 1993. I like to learn how Microsoft Systems have been built up and dig myself into different libraries in Win32 and .NET. I have worked with C++, VB6, Java, C#, ASP, ASP.Net and PHP.

You may also be interested in...


Comments and Discussions

GeneralProgressChanged event is your friend Pin
Peter Ritchie24-Jul-06 12:20
memberPeter Ritchie24-Jul-06 12:20 
For updating the UI, ProgressChanged event is your friend. It is forced to run on the same thread that created the BackroundWorker object (usually the UI thread) so you don't have to call BeginInvoke or InvokeRequired in your ProgressChanged event handler. With this event you can pass percentage (0 to signify your starting, and 100 to signify completing) and a user state object.

Peter Ritchie
Microsoft MVP, Visual Developer - Visual C#

GeneralRe: ProgressChanged event is your friend Pin
Asghar Panahy25-Jul-06 14:35
memberAsghar Panahy25-Jul-06 14:35 
GeneralRe: ProgressChanged event is your friend Pin
.hmcclungiii24-Feb-07 10:08
member.hmcclungiii24-Feb-07 10:08 
GeneralC# & Web Service Pin
sanu2021-Jul-06 0:12
membersanu2021-Jul-06 0:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 20 Jul 2006
Article Copyright 2006 by Asghar Panahy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid