Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 23455 |  Welcome, Guest!   Register  Login
Home > Articles > Visual Studio 2010/12 > Asynchronous programming - async & await

Asynchronous programming - async & await

Article posted by Ambily.Raj on 11/6/2012 | Views: 2298 | Category: Visual Studio 2010/12 | Level: Intermediate | Points: 250 red flag

Advertisements

Advertisements
.Net 4.5 introduced the new asynchronous programming model using the asyn and await keywords. Till now asynchronous programming is little complex and the debugging of the same is painful. Now with the help of the new asynchronous programming, developers can write simple codes to do the asynchronous tasks.


Asynchronous Programming

Asynchronous programming uses a single thread which will schedule a process in an asynchronous way. Program execution will continue and the UI will be responsive to the user. Typical scenario for an asynchronous programming is a long running process like data backup, bulk data processing, time consuming calculations, etc. We can schedule the time consuming work as an asynchronous task and the user can continue with his work.


Consider the following Code, where we have a set of URLs. Getting the web response and processing the same will take time. Depends on the number of URLs in the list, time taken to complete the execution will increase.

private void LongRunningMethod()
    {
        List<string> urlList = GetSetupURLList();

        foreach(var url in urlList)
        {
            byte[] urlContents = GetContents(url);
            ProcessTheContent(urlContents);

        }
    }

    private byte[] GetContents(string url)
    {
        var data = new MemoryStream();

        var webRequest = (HttpWebRequest)WebRequest.Create(url);

        using (WebResponse webResponse = webRequest.GetResponse())
        {
            using (Stream responseStream = webResponse.GetResponseStream())
            {
                responseStream.CopyTo(data);
            }

        }
        return data.ToArray();
    }


Let us see, how we can convert the same to asynchronous using the async and await keywords.


Step 1: Modify with built-in asynchronous API calls

Along with the async and await keywords, .Net 4.5 introduced a set of asynchronous APIs like StorageFile.CopyAsync(), HttpClient.DeleteAsync(), etc. Now, our method looks like 

 private byte[] GetContents(string url)
    {

        var data = new MemoryStream();

        var webRequest = (HttpWebRequest)WebRequest.Create(url);

        using (WebResponse webResponse = webRequest.GetResponseAsync())
        {
            using (Stream responseStream = webResponse.GetResponseStream())
            {
                responseStream.CopyToAsync(data);
            }
        }

        return data.ToArray();
    }   


Step 2: Use await to wait for a response

await indicate that we need the result or the processed data from the asynchronous method for further processing.


For processing the response, we need to get the web response first, so apply the “await” operator to indicate the system to wait for the result.

 

using (WebResponse webResponse = await webRequest.GetResponseAsync())

       {-----------------------------------}


Step 3:Use async to an asynchronous method

Once you add the await operator, you will get an error for the method declaration. This indicate that the method is not asynchronous and you are trying to perform an asynchronous operation from a normal method. An asynchronous method should be denoted using the async keyword. 

private async byte[] GetContents(string url)
{------------------------------------}


Step 4: Specify return type as Task/Task<type>

Now, you will get another error near the return type. An asynchronous method should return either a Task or a Task<Type> data only.

 

private async Task<byte[]> GetContents(string url)

{------------------------------------}


Step 5: Naming convention - Asynchronous method name should end with Async

Rename the method from GetContents to GetContentsAsync

 

private async Task<byte[]> GetContentsAsync(string url)

{------------------------------------}


Now, let us modify the calling method- LongRunningMethod to use our new asynchronous method.

i.       Change method name   

byte[] urlContents =GetContentsAsync(url);

ii.      Add await

byte[] urlContents = await GetContentsAsync(url);


iii.    Add async

private async void LongRunningMethod()
{…………………………………………..}

 

iv.      Modify the return type

private async Task LongRunningMethod()
{…………………………………..}

 

v.       Modify the method name

private async Task LongRunningMethodAsync()
{………………………………………….}


Now, both the methods are converted into asynchronous methods.     

  private async Task LongRunningMethodAsync()

        {

            List<string> urlList = GetSetupURLList();

            foreach (var url in urlList)

            {
                byte[] urlContents = await GetContentsAsync(url);
                ProcessTheContentAsync(urlContents);

            }
        }

        private async Task<byte[]> GetContentsAsync(string url)
        {

            var data = new MemoryStream();

            var webRequest = (HttpWebRequest)WebRequest.Create(url);

            using (WebResponse webResponse = await webRequest.GetResponseAsync())

            {
              using (Stream responseStream = webResponse.GetResponseStream())

                {

                   responseStream.CopyToAsync(data);

                }
            }

            return data.ToArray();
}


When you run the asynchronous methods, you can notice that the application is responsive even though the process is not over.


Conclusion

With the help of the .Net 4.5 asynchronous programming, we can convert a synchronous method into an asynchronous method very easily. Also the code is not so complex.
 

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Ambily KK

Experience:9 year(s)
Home page:http://ambilykk.com/
Member since:Tuesday, May 18, 2010
Level:Silver
Status: [Member] [Microsoft_MVP] [MVP]
Biography:I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.
>> Write Response - Respond to this post and get points
Related Posts

Visual Studio 2010 Productivity Power Tool is a set of extensions for Visual Studio 2010 Professional and above versions which actually improves productivity of developers. In this Tips &amp; Trick, I will show you one nice extension behavior named “Interactive Tooltip”, which will make your development life easier.

Moles are another Visual Studio add-in along with Pex. We already discussed about Pex, how to create Unit test using Pex and Parameterized Unit tests in another article.

In this article I will describe about some new features of Visual Studio 2010 which I explored till now. These features are really very useful in terms of productive development. This article is mainly targeted for beginners of Visual Studio 2010 but everybody can get benefit on the same.

Visual Studio is one of the tools used for Performance Test. Visual Studio Test edition or Visual Studio 2010 Ultimate provides the support for test automation. This article describes the web test feature available in visual studio.

In this article we will discuss about 3 important features provided by VS 2010 to ease our deployment task on production and other environment. We will first start with understanding problems with deployment and then move ahead by creating packages , one click deploy and web.config transformation.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 5:50:17 AM