Asynchronous function in c#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 4993 red flag

How to implement Asynchronious function in C# 5.0

Asynchronous function in c#

In this tutorial we will try to understand asynchronous function in C# 5.0. I hope all of you know what asynchronous technique is and how it improves performance of application. If you don’t know please go through below paragraph.

Asynchronous technique is nothing but one style to execute application. It gives the idea of parallel programming. The main advantage of asynchronous function is it does not block the user interface while it executes. 

In C# 5.0 asynchronous concept has introduce and we can implement it with the help of async and await keyword.

In this article we will create one simple console application to implement asynchronous function call. Create one console application and put below code into it.        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Asynchronious
{
    class TestAsync
    {
        public Task LongTask()
        {
            return Task.Run(() => {
                System.Threading.Thread.Sleep(5000);
            });
        }
        public async void AsynchronousFunction()
        {
            await LongTask();
            Console.WriteLine("Long Task Finished");
        }
    }
       
 
        class Program
        {
            public static void Main(String[] args)
            {
                TestAsync obj = new TestAsync();
                obj. AsynchronousFunction ();
                Console.WriteLine("Program Finish");
                Console.ReadLine();
            }
        }
}

 Here is sample output.


Lets discuss below code and try to understand why this output is coming ? If we look on output screen we will find that main function is finishing at first then LongTask() function is completing. This is the main advantage of asynchronous programming.

Let’s understand two keyword “async” and “await”.

async:-

It is used with function. If we qualify one function with async keyword then it can able to call asynchronously.   In this example we have defined the function like below.

public async void AsynchronousFunction()
        {
            await LongTask();
            Console.WriteLine("Long Task Finished");
        }

Now Asynchronousfunction() is one asynchronous function in nature.

await:-

Within any asynchronous function we can use await keyword. When we want to call any long running process we can call it using await keyword. It implies that the current thread will not wait until the process finish. We are calling the LongTask() function with await keyword.

await  LongTask().

Now, Lets understand LongTask() function. Here is the body of LongTask() function.

        public Task LongTask()
        {
            return Task.Run(() => {
                System.Threading.Thread.Sleep(5000);
            });
        }

It is returning Task. Task is nothing but one unit of work. We are making delay by calling sleep() function of Thread class.

Conclusion:-

In this article we have seen how to implement asynchronous function in c# 5.0. In coming up article we will explore more on same topic.



Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)