Click here to Skip to main content
13,293,178 members (85,945 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 15 Aug 2008

How to call a long running method asynchronously and display a wait image on the UI

, 27 Aug 2008
Rate this:
Please Sign up or sign in to vote.
This article explains how to call a long running method asynchronously and also display a wait image on the UI.


This article describes how to call a long running method asynchronously on a separate thread and also display a wait image until the thread is not done executing a task.


There have been many situations where developers have to run a function/method/programming logic that takes substantially longer than usual. In that case, it's always recommended to run it asynchronously in a separate thread and also display a wait message/image to the end user. This sample application will explain how this can be achieved.

Using the Code

First, I have declared two delegates in class Form1. LoadDataDelegate is used to call the long running method asynchronously, and it takes the key as a parameter (just to show a parameterized sample). DisplayWaitDelegate is used to display/hide the wait image. It takes a boolean parameter to set whether to display or hide.

//declare a delegate to run the Load method in a separate thread
delegate void LoadDataDelegate(string key);
//declare a delegate to call the Display Wait method
delegate void DisplayWaitDelegate(bool boolDisplay);

The following code is in my Load Data button click event. I will explain the function DisplayWait later, but this is used to display the wait image. The function "LoadData" in Class1 takes longer to execute, so I have instantiated the LoadDataDelegate delegate and passed in this function name. Finally, call BeginInvoke to start. Notice the second parameter which is a function name (which is explained later) is called when this asynchronous call / thread is completed.

private void btnLoadData_Click(object sender, EventArgs e)
    //will be calling the long running methid, so diaplay the wait image
    string key = textBox1.Text;
    Class1 objClass1 = new Class1();

    //Create an instance of the Load delegate and pass in the Function Name
    LoadDataDelegate delLoadData = new LoadDataDelegate(objClass1.LoadData);

    //Since we want to call the Load function
    //asynchronously on another thread, use BeginInvoke. The next
    //argument is the function name being called once this thread is completed
    delLoadData.BeginInvoke(key, this.LoadComplete, delLoadData);

Following is the code for the LoadComplete function. This function gets the handle for the delegate and calls the EndInvoke method to stop the asynchronous call. The function DisplayWait is called again, but this time with the parameter boolean value as false, which hides the wait image.

private void LoadComplete(IAsyncResult ar)
    LoadDataDelegate d = (LoadDataDelegate)ar.AsyncState;

    //end the Load method call

    //now when the Load is complete..remove the wait image

    MessageBox.Show("Data Load Completed.");

Following is the code for the DisplayWait function. The pcitureBox1 control contains the wait image. The InvokeRequired call is needed to ensure this program doesn't cause any cross thread exception. Again, create an instance of DisplayWaitDelegate and pass the same method name "DisplayWait". Then call the Invoke method of the control (in this case, pictureBox1) and pass the delegate instance and parameters in an array of objects (in this case, just one parameter).

Now, this will call the function DisplayWait again, and this time, it will go in the else block, and there I'm setting up the visibility. I'm also setting up the UseWaitCursor property at the Form level to display the hour glass when the wait image is being displayed.

private void DisplayWait(bool boolDisplay)
    //this check is required to carry out the cross thread 
    //oprtation on a control which is pictureBox1 in this case.
    if (pictureBox1.InvokeRequired)
        // this is worker thread
        DisplayWaitDelegate del = new DisplayWaitDelegate(DisplayWait);
        pictureBox1.Invoke(del, new object[] { boolDisplay });
        // this is UI thread 
        pictureBox1.Visible = boolDisplay;
        UseWaitCursor = boolDisplay;

Now the last but not the least, the function "LoadData" in Class1, which is nothing but Thread.Sleep(10000) to show a sample delay of 10 seconds with this application.

//long running method 
public void LoadData(string key) 
    // The actual method imlementation
    //just for sample delay added the sleep for 10 seconds

Points of Interest

This can also be extended to show a progress bar.


  • Version 1.0: 08/22/2008.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Member 2941560
Software Developer (Senior) Nu Info Systems
United States United States
I have worked extensively in microsoft technologies for over 8 years now. I have worked on some pretty big size development projects and love progamming in C#, .Net. In my spare time I love reading, blogging and spending time with family.

You may also be interested in...


Comments and Discussions

Generalbundle of thanks Pin
Member 40697249-Sep-12 17:45
memberMember 40697249-Sep-12 17:45 
Questioncan I use this in web application? Pin
mariagp17-Oct-08 10:33
membermariagp17-Oct-08 10:33 
GeneralUse BackGroundWorker Pin
Abhijit Jana27-Aug-08 18:41
memberAbhijit Jana27-Aug-08 18:41 
RantRe: Use BackGroundWorker Pin
Ilíon28-Aug-08 2:21
memberIlíon28-Aug-08 2:21 
GeneralBackgroundThread Pin
Itay Sagui27-Aug-08 7:41
memberItay Sagui27-Aug-08 7:41 
GeneralRe: BackgroundThread Pin
marco_br27-Aug-08 8:12
membermarco_br27-Aug-08 8:12 
GeneralRe: BackgroundThread Pin
CIDev27-Aug-08 10:48
memberCIDev27-Aug-08 10:48 
RantBackgroundThread ... useful if you have it Pin
Ilíon28-Aug-08 2:20
memberIlíon28-Aug-08 2:20 
But what about those ... such as I ... who do not have VS2005 or higher?

I have to "roll my own." And to do that I need others' examples to help me learn what's required.
GeneralRe: BackgroundThread ... useful if you have it Pin
Roberto Collina28-Aug-08 4:20
memberRoberto Collina28-Aug-08 4:20 
GeneralRe: BackgroundThread ... useful if you have it Pin
Ilíon28-Aug-08 5:16
memberIlíon28-Aug-08 5:16 

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
Web02 | 2.8.171207.1 | Last Updated 27 Aug 2008
Article Copyright 2008 by Member 2941560
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid