Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • async & await keyword in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 340
    Comment on it

    async & await keyword in C#

     

    Asynchronous programming can be performed in C# using async & await keyword. These keywords were introduced in C# 5. These keywords are to be used together i.e await keyword can only be used within async method while async method without await keyword within behave synchronously.   

     

    async keyword

     

    A method is said to be an async method when async method(caller method) return control of execution to the calling method before completing it's execution. The caller method completes it's execution while calling method continues with it's execution. A method with an async keyword before return type in method header is an async method. The job of async method is just that it is container of one or more await expressions. These expressions are representing task which are to be done asynchronously.

     

    await keyword

     

    The await keyword is used within async method. This await keyword is defining a suspension point which tells the compiler that execution of async method is suspended i.e execution can't continue pass that suspension point unless the asynchronous process with which await keyword is used is completed. Meanwhile, execution control is returned to the caller method of async method causing caller method to continue with it's execution while async method is running in background. When asynchronous process is finished within it's execution, execution control is again returned from caller method to async method. An async method without await keyword is executed similarly as synchronous method irrespective of async keyword. A warning is thrown by compiler for such methods.

     

    Program with async method using await keyword behave as asynchronously :-

     

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace DemoApplication
    {
     class program
        {
            static void Main(String[] args)
            {
                Task<int> demo = AsynchronousDemo.CalculatingSum(2, 3);
                Console.WriteLine("In Main method");
                Console.WriteLine("In Main method");
                Console.ReadKey();
            }
    
            class AsynchronousDemo
            {
                public static async Task<int> CalculatingSum(int i, int j)
                {
                    int sum = await Task.Run(() => GetSum(i, j));
                    Console.WriteLine("Value: {0}", sum);
                    return sum;
                }
    
                private static int GetSum(int i, int j)
                {
                    return i + j;
                }
            }
         }
    }  

     

    Output :

     


       

    In above output it can be seen that in async method whenever await is encountered a suspension point is generated execution control returned to caller method thus continuing with execution and when asynchronous process finish execution control is returned back to async method.

    Program with async method without using await keyword behave as synchronous method :-

     

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace DemoApplication
    {
      class program
        {
            static void Main(String[] args)
            {
                Task<int> demo = AsynchronousDemo.CalculatingSum(2, 3);
                Console.WriteLine("In Main method");
                Console.WriteLine("In Main method");
                Console.ReadKey();
            }
    
            class AsynchronousDemo
            {
                public static async Task<int> CalculatingSum(int i, int j)
                {
                    int sum = GetSum(i, j);
                    Console.WriteLine("Value: {0}", sum);
                    return sum;
                }
    
                private static int GetSum(int i, int j)
                {
                    return i + j;
                }
            }
         }
    }    

     

     

    Output :

     

     

    In above output since await is not used within async method therefore async method behave synchronously, it first finish all it's work then return execution control back to caller method(Main). 

     

     

     

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: