Preface
In C # 5.0, the async await 2 keywords are added to support asynchronous programming operations. Before I tell you about these two keywords, let me summarize. NET, the common asynchronous programming model.
Asynchronous programming has always been a complicated problem, in which we need to design a programming model to deal with the problem, such as synchronizing data, getting progress, canceling, obtaining results, not affecting the main thread operation, and not affecting each other.
starting with. NET 4.5, three asynchronous programming modes supported:
Event-based Asynchronous Programming design pattern (eap,event-based asynchronous patterns)
Asynchronous programming models (Ape,asynchronous programming model)
Task-based programming model (tap,task-based Asynchronous Pattern)
The current version of the. NET is biased to recommend the use of TAP mode for asynchronous programming, WINRT in the asynchronous operation is only tap, async await keyword is only a programming model to support tap.
event-based Asynchronous Pattern-EAP
The code for the EAP programming model has the following characteristics:
There will be one or more methods named [Method name]async]. These methods may create a mirrored version of the synchronization that performs the same action on the current thread.
The class may also have a [method name]completed] event that listens for the results of the asynchronous method.
It may have a "[Method name]asynccancel" (or just CancelAsync) method that cancels an asynchronous operation in progress.
Here is an example of a class sound that conforms to this pattern
Copy Code code as follows:
public class Asyncexample
{
Synchronous methods.
public int Method1 (string param);
public void Method2 (double param);
Asynchronous methods.
public void Method1Async (string param);
public void Method1Async (string param, object userState);
public event Method1completedeventhandler method1completed;
public void Method2async (double param);
public void Method2async (double param, object userState);
public event Method2completedeventhandler method2completed;
public void CancelAsync (object userState);
public bool IsBusy {get;}
Class implementation not shown.
}
The fictitious Asyncexample class here has two methods that support both synchronous and asynchronous invocations. Synchronous overloading behaves like method calls, which perform actions on the calling thread, and if the operation is time-consuming, the return of the call can be significantly delayed. The asynchronous overload initiates the operation on another thread and returns immediately, allowing the operation to be performed "in the background" while the calling thread continues to execute.
System.Net.WebClient itself has many examples of EAP, with its downloadstring as an example, WebClient
the downloadstring related methods are:
Downloadstring: Synchronizes the method of downloading a string resource, which blocks the current thread.
DownloadStringAsync: Method of downloading string resources using EAP asynchronous Programming mode, this method does not block the current thread.
Downloadstringcompleted: An event that is completed in response to an asynchronous download.
DownloadProgressChanged: Progress changes in response to asynchronous downloads.
Call Model example below:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Net;
Namespace Asynctest1.eap
{
public class EAPRunTest1
{
public static void Asyncrun () {
Utility.Log ("Asyncrun:start");
Test URLs
String url = "http://sports.163.com/nba/";
using (WebClient WebClient = new WebClient ()) {
Monitor download Progress
Webclient.downloadprogresschanged + = new Downloadprogresschangedeventhandler (webclient_downloadprogresschanged);
Monitoring of completion
webclient.downloadstringcompleted + = new Downloadstringcompletedeventhandler (webclient_downloadstringcompleted);
Webclient.downloadstringasync (new Uri (URL));
Utility.Log ("Asyncrun:download_start");
}
}
static void Webclient_downloadstringcompleted (object sender, DownloadStringCompletedEventArgs e)
{
String log = "asyncrun:download_completed";
Log = + "|cancel=" + e.cancelled.tostring ();
if (e.error!= null)
{
An exception is logged, and an exception is recorded
Log = + "|error=" + e.error.message;
}
else {
If no exception occurs, the result is logged
Log = + "|result_size=" + Utility.getstrlen (E.result);
}
Utility.Log (LOG);
}
static void Webclient_downloadprogresschanged (object sender, DownloadProgressChangedEventArgs e)
{
Utility.Log ("asyncrun:download_progress|percent=" + e.progresspercentage.tostring ());
}
}
}
Run Results:
2012-12-28 00:39:39:621 Asyncrun:start
2012-12-28 00:39:40:377 Asyncrun:download_start
2012-12-28 00:39:40:903 Asyncrun:download_progress|percent=1
2012-12-28 00:39:40:933 asyncrun:download_progress|percent=3
2012-12-28 00:39:40:933 asyncrun:download_progress|percent=5
2012-12-28 00:39:40:934 asyncrun:download_progress|percent=5
2012-12-28 00:39:40:975 asyncrun:download_progress|percent=9
2012-12-28 00:39:41:068 asyncrun:download_progress|percent=21
2012-12-28 00:39:41:131 asyncrun:download_progress|percent=29
2012-12-28 00:39:41:182 asyncrun:download_progress|percent=37
2012-12-28 00:39:41:298 asyncrun:download_progress|percent=50
2012-12-28 00:39:41:354 asyncrun:download_progress|percent=58
2012-12-28 00:39:41:447 asyncrun:download_progress|percent=74
2012-12-28 00:39:41:489 asyncrun:download_progress|percent=82
2012-12-28 00:39:41:582 asyncrun:download_progress|percent=100
2012-12-28 00:39:41:582 asyncrun:download_progress|percent=100
2012-12-28 00:39:41:614 asyncrun:download_completed|cancel=false|result_size=205568