I have written two articles on asynchronous programming, which can be entered into c#~ asynchronous programming and c#~ asynchronous programming in project use
Each version of. NET has its own master-push technology, like a delegate in. NET1.1, a generic in. NET2.0 in. NET3.0, and Dynimac in the linq,.net4.0 in. The main thing in NET4.5 is asynchronous programming, and the simplest way to implement asynchronous programming is to use the await and async keywords, which are already used in the new. NET Managed class library, and we can use it in the vs2010 add patch and vs2012. In Microsoft's battlefield, we have never seen asynchronous programming so prevalent in the launch. net4.5, so the need for asynchronous methods, are implemented in synchronous and asynchronous two versions, which gives developers a great convenience!
Here are two examples to illustrate how an await and async are used
A remote image is fetched and then responded to on the browser
#region asynchronously returns the file stream (increasing the throughput capacity of the application) public async task<actionresult> awaitfile () { var responsehtml = await GetResponseStream ("http://static.xuexiba.com/uploadfile/UserInfo/Avatar/201311/1302844759237319155175.jpg"); Return File (responsehtml, "image/jpeg"); } Private Async task<stream> getresponsestream (string url) { return await getresponsecontentasync (URL); } Private Async task<stream> getresponsecontentasync (string url) { var httpClient = new System.Net.Http.HttpClient (); var response = await httpclient.getasync (URL); if (response. StatusCode = = System.Net.HttpStatusCode.OK) { return await response. Content.readasstreamasync (); } else { return null;//error } } #endregion
Two to get the remote HTML file and then respond on the browser
#region asynchronously returns text (improves application throughput) public async task<actionresult> awaithtml () { var responsehtml = await Getresponsehtml ("http://www.sina.com"); Return Content (responsehtml); } Private Async task<string> getresponsehtml (string url) { return await getresponsestringasync (URL); } Private Async task<string> getresponsestringasync (string url) { var httpClient = new System.Net.Http.HttpClient (); var response = await httpclient.getasync (URL); if (response. StatusCode = = System.Net.HttpStatusCode.OK) { return await response. Content.readasstringasync ();//plus await means that the main UI waits for it to finish before it continues execution, which is called parallelism! } else { return null;//error } } #endregion
And after the run, we can see that it is normal output, and in its operation behind, we may not know that it is implemented on the main path of the WWW is not run, but asynchronously opened a new thread to do this thing, it is important, it does so for the purpose of only one, that is to engage in high-www response ability, That is, improve the throughput of its programs!
Original link: http://www.cnblogs.com/lori/p/3955868.html
c#~ Asynchronous Programming Continuation ~.net4.5 Await&async application of the main push (GO)