Asynchronous and wait (async and await), asyncawait

Source: Internet
Author: User

Asynchronous and wait (async and await), asyncawait

In. Net 4.5, a new task-based asynchronous programming model (TAP) is introduced through the async and await keywords ). In this way, asynchronous code can be written in a similar synchronous mode, greatly simplifying the asynchronous programming model. A simple example is as follows:

static async void DownloadStringAsync2(Uri uri)    {        var webClient = new WebClient();        var result = await webClient.DownloadStringTaskAsync(uri);        Console.WriteLine(result);    }

The previous method is as follows:

static void DownloadStringAsync(Uri uri)    {        var webClient = new WebClient();        webClient.DownloadStringCompleted += (s, e) =>            {                Console.WriteLine(e.Result);            };        webClient.DownloadStringAsync(uri);    }

Perhaps the previous example is insufficient to reflect the superiority brought by async and await. The following example is much more obvious:

public void CopyToAsyncTheHardWay(Stream source, Stream destination)    {        byte[] buffer = new byte[0x1000];        Action<IAsyncResult> readWriteLoop = null;        readWriteLoop = iar =>        {            for (bool isRead = (iar == null); ; isRead = !isRead)            {                switch (isRead)                {                    case true:                        iar = source.BeginRead(buffer, 0, buffer.Length,                            readResult =>                            {                                if (readResult.CompletedSynchronously) return;                                readWriteLoop(readResult);                            }, null);                        if (!iar.CompletedSynchronously) return;                        break;                    case false:                        int numRead = source.EndRead(iar);                        if (numRead == 0)                        {                            return;                        }                        iar = destination.BeginWrite(buffer, 0, numRead,                            writeResult =>                            {                                if (writeResult.CompletedSynchronously) return;                                destination.EndWrite(writeResult);                                readWriteLoop(null);                            }, null);                        if (!iar.CompletedSynchronously) return;                        destination.EndWrite(iar);                        break;                }            }        };        readWriteLoop(null);    }    public async Task CopyToAsync(Stream source, Stream destination)    {        byte[] buffer = new byte[0x1000];        int numRead;        while ((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) != 0)        {            await destination.WriteAsync(buffer, 0, numRead);        }    }

For more information, see Microsoft's official website: Visual Studio Asynchronous Programming. Its official documentation, Task-Based Asynchronous Pattern Overview, describes CSharp Language Specification in Visual Studio.

 

Source: http://blog.csdn.net/wulex/article/details/73499500

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.