Showing posts with label asynchronous programming in c#. Show all posts
Showing posts with label asynchronous programming in c#. Show all posts

Async await in c#

What is async await in c#?


You might have already heard about asynchronous programming, either in C# or in some other language. In this page I will try to explain it to you, how to understand the concept easily, why and when we write asynchronous, the structure of async / await.

Asynchronous programming is very helpful in this condition. By using Asynchronous programming, the Application can continue with the other work that does not depend on the completion of the whole task.if any task depends on another task then next task will not execute until first task not completed.

Async and await are the code markers, which marks code positions from where the control should resume after a task completes.

We will get all the benefits of traditional Asynchronous programming with much less effort by the help of async and await keywords.

Let's take example so understand it properly

class Proram
    {
        public static void Main()
        {
       
            ProcessOne();
            IndependentProcess();//this process will run separately
            Console.ReadLine();
        }
        public async static void ProcessOne()
        {
            /*
             You can see that we mark DependedProcess method as await
             means untill DependedProcess will not complete next line will not called
            */
            await Task.Run(new Action(DependedProcess));
            Console.WriteLine("Process One Called");
        }
     
        public static void DependedProcess()
        {
            Thread.Sleep(2000);
            Console.WriteLine("Process Two Called");
        }
        public static void IndependentProcess()
        {
            Console.WriteLine("Independent Process");
        }
    }

Output:
Independent Process
Process Two Called
Process One Called

Summary: In above program you can see that Once DependentProcess method complete process after that next line of code will execute.while Independentprocess method will run separately this is called the Asynchronous programming.



















Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...