Click here to Skip to main content
Click here to Skip to main content

Asynchronous methods using C#: Part I

, 22 Apr 2012
Rate this:
Please Sign up or sign in to vote.
In this article we will create a method that works asynchronously.

Introduction

In this article, we’ll see how to write an asynchronous method. For better understanding, first we’ll perform a task conventionally and then we’ll create its async version.

Objective

To write an asynchronous method.

Background

Let us first design a class Task and write a Perform() method that iterates loops over some collection.

class Task
{
    public void Perform(string[] files)
    {
        foreach (string file in files)
        {
            //do something......
            Thread.Sleep(100);
        }
    }
}

In the above example, we are iterating a loop for an array of strings. This is the conventional method which we all normally write. Now let’s try to perform the same task asynchronously using delegates. For this, we need the following members:

  • Perform() method – does actual work.
  • PerformAsync() method – invokes the asynchronous operation.
  • CompleteTask() method – called when perform method completed operation.
  • IsBusy property – indicates that an asynchronous operation is going on.
  • TaskCompleted event – notifies of the asynchronous operation completion.

Using the code

Perform() method:

This method executes in the background and does the actual task. In our example, it iterates a loop over an array of strings.

// Here is a method, that does the actual work
private void Perform(string[] files)
{
    foreach (string file in files)
    {
        //do something......
        Thread.Sleep(100);
    }
}
// delegate that will invoke the method asynchronously.
delegate void TaskEventHandler(string[] files, 
         AsyncOperation async, SendOrPostCallback callback);

PerformAsync() method:

This method invokes an asynchronous operation and returns immediately. If an asynchronous operation is running, then throw an exception.

public void PerformAsync(string[] files)
{
    if (IsBusy)
        throw new InvalidOperationException("The worker is busy.");

    async = AsyncOperationManager.CreateOperation(this);

    TaskEventHandler handler =
        new TaskEventHandler(ProcessWorker);
    handler.BeginInvoke(files, async, CompleteTask, null, null);
}

First it checks for busy status of operation, if it’s so, then throw InvalidCastOperationException. Next an AsyncOperation is created. This object is used by a worker thread to invoke the client’s event handler on a proper thread. Next TaskEventHandler is registered with a proper handler method. Through TaskEventHandler, we can invoke our Task asynchronously. Now the asynchronous operation is started in a separate thread using the handler.BeginInvoke method. The BeginInvoke method contains two more parameters along with a string array.

  1. A delegate of type SendOrPostCallback to a callback method which is called when operation finishes.
  2. A custom object that is stored in the AsyncState property of an IAsyncResult instance returned by the BeginInvoke method.

CompleteTask() method:

This method is called when a worker invoker has finished its processes.

void CompleteTask(object state)
{
    object[] args = (object[])state;
    RunWorkerCompletedEventArgs e =
        args[0] as RunWorkerCompletedEventArgs;
    AsyncOperation async = args[1] as AsyncOperation;

    SendOrPostCallback callback = delegate(object darg)
    {
        OnRunWorkerCompleted(darg as RunWorkerCompletedEventArgs);
    };

    async.PostOperationCompleted(callback, e);

    this.async = null;
}

First the AsyncOperation object is obtained and at last the TaskCompleted event is fired through the AsyncOperation object.

TaskCompleted Event:

This event is fired when the asynchronous operation completes.

public event RunWorkerCompletedEventHandler TaskCompleted
{
    add
    {
        this.taskCompletedEventHandler += value;
    }
    remove
    {
        this.taskCompletedEventHandler -= value;
    }
}
private RunWorkerCompletedEventHandler taskCompletedEventHandler;

protected virtual void OnTaskCompleted(RunWorkerCompletedEventArgs e)
{
    if (taskCompletedEventHandler != null)
        taskCompletedEventHandler(this, e);
}

In this Series

  1. [C#] Asynchronous method using C#: Part I
  2. [C#] Asychronous method using C#: Part II

License

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
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMauro Leggieri14-May-14 4:30 
QuestionInternet PinmemberRandalthor30-Jan-13 0:57 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 22 Apr 2012
Article Copyright 2012 by Himanshu Manjarawala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid