Click here to Skip to main content
13,047,502 members (57,263 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 21 Apr 2012

Asynchronous method using C#: Part II

, 21 Apr 2012
Rate this:
Please Sign up or sign in to vote.
How to report progress and intermediate results from an asynchronous process to client code.

Sample Image


In this article, we demonstrate how to report progress information and intermediate results from an asynchronous method. This article builds on the previous article, Asynchronous method using C#: Part I.

Using the code

In this article, we do the following changes to our previous example:

  • Modify the Perform method for reporting progress.

  • Implement the ProgressChanged event.

  • Define the method ReportProgrss that synchronizes the Perform method and the ProgressChanged event.

The Perform method

Here is the Perform method that we define in our previous example.

private void Perform(string[] files)
    foreach (string file in files)
        //do something......

Now we modify it in such a way that it reports the progress of the task to the client code and the processed files.

private void Perform(string[] files)
    int counter = 0;
    foreach (string file in files)
        //do something......

        //compute progress
        int percentage = (100 * counter) / files.Length;
        //raises progresschanged event
        ReportProgress(percentage, file);

After processing the file, we compute the percentage in percentage and pass it as an argument in the ReportProgress() method, which is an example of how to pass intermediate results to the client code.

Implement the ProgressChanged event

This event is raised whenever an asynchronous method wants to report progress to client code.

public event ProgressChangedEventHandler ProgressChanged
        this.progressChangedEventHandler += value;
        this.progressChangedEventHandler -= value;
private ProgressChangedEventHandler progressChangedEventHandler;

protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
    if (progressChangedEventHandler != null)
        progressChangedEventHandler(this, e);

Handling the ProcessChanged event in client code

Register and implement a handler of the ProgressChanged event.

t.ProgressChanged += new ProgressChangedEventHandler(task_ProgressChange);

static void task_ProgressChange(object sender, ProgressChangedEventArgs e)
    Console.WriteLine("[Task] Progress: {0} %, Current File: {1}", 
                      e.ProgressPercentage, e.UserState);

The ReportProgress method

This method calls the ProgressChanged event through an AsyncOperation object.

void ReportProgress(int percentProgress, object userState)
    // FIXME: verify the expected behavior
    if (!IsBusy)

    async.Post(delegate(object o)
        ProgressChangedEventArgs e = o as ProgressChangedEventArgs;
        new ProgressChangedEventArgs(percentProgress, userState));

In this series:

  1. Asynchronous method using C#: Part I.

  2. Asynchronous method using C#: Part II.


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


About the Author

Himanshu Manjarawala
Team Leader Automation Anywhere Inc.
India India
I am Himanshu Manjarawala, Garduate in Computer Science and MCA From Veer Narmad South Gujarat University, Surat Gijarat India. Currently working as Sr. Software Developer in Automation Anywhere Softwares Pvt. Ltd. Vadodara, Gujarat

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 21 Apr 2012
Article Copyright 2012 by Himanshu Manjarawala
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid