C #~ Asynchronous programming continued ~. Net4.5 the await & amp; async application,

Source: Internet
Author: User

C #~ Asynchronous programming continued ~. Net4.5 main await & async applications,

I have written two articles about asynchronous programming before. For details, go to C #~ Asynchronous programming and C #~ Use of asynchronous programming in projects

. Each version of net has its own proprietary technology, such. delegate in NET1.1 ,. generic in NET2.0 ,. in NET3.0 ,. dynimac in NET4.0, while in. in NET4.5, asynchronous programming is mainly introduced. The simple method to implement asynchronous programming is to use the await and async keywords. these two keywords have been greatly used in the net hosting class library. We can use them in vs2010 patch and vs2012. On the Microsoft battlefield, we have never seen asynchronous programming become so popular, coming soon. after net4.5, asynchronous methods are required, both of which are implemented in synchronous and asynchronous versions, which greatly facilitates developers!

Here are two examples to illustrate how to use await and async.

1. Obtain the remote image and then respond in the browser
# Region asynchronous return of File streams (improving application throughput) 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
2. Obtain the remote HTML file and then respond to it in the browser.
# Region asynchronous return of text (improving 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 () ;}else {return null; // error }}# endregion

After running, we can see that it is output normally, but behind it, we may not know that it is not running on the WWW main thread, it is important to enable a new thread asynchronously to do this. The only purpose of doing so is to improve WWW response capabilities, that is, to increase the program throughput!


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.