Saturday, August 6, 2016

1. Asynchronous programming using Async and Await


Asynchronous programming makes it possible to run multiple operation concurrently, In other words Asynchronously.
Dot Net framework 4.0 supports Task based asynchronous programming model.
We will see by example how to implement asynchronous programming using async and await keyword.
suppose we have a long running operation named M1(), like:

      public static void M1()
       {
           for (int i = 0; i < 10000; i++)
           {
               Console.WriteLine(" From M1, i=" + i);
           }
       }
Above mentioned method is our actual operation which we want to run asynchronously. in this operation I used static keyword because I want to use this operation in Main method which is static.

Now we will create a new method  M1Wrapper() which will use async and await keyword, this new method will call M1() method and uses async,await and Task keywords like:

        public async static Task M1Wrapper()
        {
            await Task.Run(() => M1());
        }

To implement M1Wrapper() you need to remember :

  • Name of method : just for understanding purpose I am using method name as M1Wrapper but as a standard practice name should be M1Async to represent asynchronous operation.
  • you should use await followed by Task.Run method to run operation asynchronously.I am using non generic version of Run because it's (M1) return type is void. 
  • In method signature I have used async keyword which is necessary to make operation asynchronous.
  • Note that I am using Task as return type but it's not necessary if your operation returns void, but I used Task because I want to wait until this asynchronous operation will complete.it's possible by calling Task.Wait() method.(see complete code below to understand this concept in detail.) 
  • In case your method returns any other type then you have to use Task.Run  where T is return type.
  • You can use any no of input parameter in async method but remember that you can not use ref and out parameter in asynchronous method. 
  • Instead of creating a method M1 and then async wrapper method (M1Wrapper), you can put M1's code inside of Task.Run() method in-place of M1(). To make things easier to understand I created separate methods.
  • An async method that has a void return type can’t be awaited, and the caller of a void-returning method can't catch any exceptions that the method throws.

 Complete Code:

         public static void Main()
        {            
            Task t = M1Wrapper();

            //Other Operation ....

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Main i=" + i);
            }
            t.Wait();
            Console.ReadKey();
        }
        public async static Task M1Wrapper()
        {
            await Task.Run(() => M1());
        }

        public static void M1()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(" From method M1: i=" + i);
            }
        }

Result :









No comments:

Post a Comment

C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...