Async and Await in C # 5.0 (Finishing ... )

Source: Internet
Author: User

Async and Await in C # 5.0

"Bo Master" anti-bone Aberdeen "This article" http://www.cnblogs.com/liqingwen/p/6069062.html

Along with the. NET 4.5 and Visual Studio 2012 C # 5.0, we can use the new async pattern, which involves the async and await keywords. There are many different points of view, and it is more readable and useful than the code we have seen before. We will use an example to see how it is "different" from the current practice.

Linear Code and nonlinear code

Most software engineers are accustomed to programming in a linear way of thinking, perhaps as they have been taught since they started their careers. When you choose to use linear thinking to write a program, this means that its source code in reading is a bit like Figure 1. This assumes that we have an order system that will help us get a batch of orders from somewhere.

  

Even if the article starts from the left or right, we are accustomed to reading from the top down. If there is something that affects the order of this content, we will be confused and spend more time and energy on it, even if it is not necessary. Event-based applications typically have these non-linear structures.

An event-based system process that, while triggering, expects to return a result, as shown in Figure 2. At first glance, these two sequences seem to have little difference, but if we assume that getallorders returns void, then retrieving the list of orders is not straightforward.

Without looking at the actual code, we think that the linear approach is more comfortable to handle and less prone to error. In this case, the error may not be a run-time error or a compile-time error, but rather an error when used: Due to a lack of sober awareness.

The event-based approach also has a big advantage, which makes us more consistent with the use of event-based asynchronous patterns.

When you see a method, you have an impulse to figure out how to do it. This means that if you have a method called Reloadordersandrefreshui, you'll want to figure out how to load the order, how to add it to the UI, and what happens when the method ends.  In the event-based approach, this is more difficult to implement. Another benefit is that as long as we trigger the loadorderscompleted event, we are able to write asynchronous code in Getallorders and go back to the calling thread.

Introduction of a new pattern

Suppose that we work on our own systems, the system uses the Orderhandler mentioned above, and in practice is a linear approach. To simulate a small part of the real order system, Orderhandler and order are as follows:

1 classOrder2 {3      Public stringOrderNumber {Get;Set; }4      Public decimalOrderTotal {Get;Set; }5      Public stringReference {Get;Set; }6 }7 classOrderhandler8 {9     Private ReadOnlyIenumerable<order>_orders;Ten      PublicOrderhandler () One     { A_orders =New[] -                 { -                     NewOrder {ordernumber ="F1", OrderTotal = -, Reference ="Filip"}, the                     NewOrder {ordernumber ="F1", OrderTotal = -, Reference ="Filip"} -                 }; -     } -      PublicIenumerable<order>getallorders () +     { -         return_orders; +     } A}

Because we don't use the real data source in the example, we can add a bit of fun to it. This is about asynchronous programming, and we want to ask for something in an asynchronous way. In order to simulate this, we simply added:
1 System.Threading.ManualResetEvent (false). WaitOne (  getallorders:2 public ienumerable<order>  Getallorders ()3{4     System.Threading.ManualResetEvent (false). WaitOne (+); 5     return _orders; 6 }

--in the finishing--

"Original" Http://www.dotnetcurry.com/csharp/869/async-await-csharp-dotnet

Async and Await in C # 5.0 (Finishing ... )

Related Article

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.