How To Call Asynchronous Method C#

In this article, we will learn how to call asynchronous method C#

Suppose we have a function Print() that is taking one second to execute and this function is called by the main function (calling function).

public void print()
{
    Thread.Sleep(3000);
    Console.WriteLine("Hello World");
}

After three second it will print “Hello World” and the control will return to the calling function. This is called a synchronous call. In an asynchronous call, rather than waiting for the code to complete, the call returns immediately to the calling function.

Example

public class Program
{
    public delegate void testMethod();
    static void Main(string[] args)
    {
        var val = new testMethod(print);
        val.BeginInvoke(null, null);
        Console.WriteLine("Testing");
        Console.Read();
    }
    public static void print()
    {
        Thread.Sleep(3000);
        Console.WriteLine("Hello World");
    }
}

I have declared a delegate and created an object of it in the main function. The delegate will call the print method. Here I have used BeginInvoke() rather than using Invoke() because if I would have used Invoke it would again be a synchronous call. BeginInvoke() returns an IAsynchResult object and this is the thing that really binds EndInvoke() and BeginInvoke().

public class Program
{
    public delegate void testMethod();
    static void Main(string[] args)
    {
        var obj = new testMethod (print);
        IAsyncResult tag = obj.BeginInvoke(null, null);
        Console.WriteLine("I am not completed yet");
        obj.EndInvoke(tag);
        Console.WriteLine("Execution Over");
    }
    public static  void print()
    {
        Thread.Sleep(3000);
        Console.WriteLine("Hello World");
    }
}

It is quite simple to declare a parameter at the same time as the declaration of the delegate. During invocation of the delegate we will pass the value. In my example I am passing “Hello World”.

public delegate void testMethod(string abc);
static void Main(string[] args)
{
    var obj = new testMethod (print);
    IAsyncResult tag = obj.BeginInvoke("Hello World",null, null);
    Console.WriteLine("I am not completed yet");
    obj.EndInvoke(tag);
    Console.WriteLine("Execution Over");
}
public static void print(string param)
{
    Thread.Sleep(3000);
    Console.WriteLine(param);
}

Now create an instance of the AsyncCallback delegate and pass it to the BeginInvoke method. When you pass an AsyncCallback to BeginInvoke, the delegate will invoke that callback upon completion of the asynchronous operation. Then, you can use that opportunity to call EndInvoke to retrieve the return value.

public class Program
{
    public delegate DataTable testMethod(string s);
    public static DataTable dt;
    public static testMethod inv;
    static void Main(string[] args)
    {
        inv = new testMethod(Print);
        inv.BeginInvoke("Priyank", new AsyncCallback(Callback), null);
        Console.ReadLine();
    }
    public static DataTable Print(string q)
    {
        Thread.Sleep(3000);
        Console.WriteLine(q);
        DataTable dt = new DataTable();
        dt.Columns.Add("Age");
        dt.Rows.Add(1);
        dt.Rows.Add(2);
        dt.Rows.Add(3);
        return dt;
    }
    public static void Callback(IAsyncResult t)
    {
        dt = inv.EndInvoke(t);
        foreach (DataRow row in dt.Rows)
        {
            Console.WriteLine(row["age"].ToString());
        }
    }
}

Output

Priyank
1
2
3

Hope you understand the article , If you still have any questions or queries then please let me know in the comment section, I’ll respond to you as soon as possible.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories