The content of this article mostly comes from the colleague's message description, the thought oneself uses the concise language to report, but the language level is limited, and the colleague's introduction is also extremely detailed and clear, therefore no longer superfluous, the direct quote ....
By using asynchronous programming, you can avoid performance bottlenecks and enhance the overall responsiveness of your application. However, traditional techniques for writing asynchronous applications can be complex, making them difficult to write, debug, and maintain.
Visual Studio 2012 introduces a simplified method of asynchronous programming that leverages asynchronous support in the. NET Framework 4.5 and the Windows runtime. The compiler can perform the hard work that the developer has done, and the application retains a logical structure similar to the synchronous code. so you can get all the benefits of asynchronous programming with just a small amount of work.
For the project I am doing, the main is to write Webapi, some of the interfaces of the return value of the processing is particularly complex, such as: need to take the data in several data sources, and then may also be based on the business of Big data processing and analysis, and so on, the execution efficiency is not so fast, and these operations are done synchronously, Includes some resource-intensive operations, and if multiple WEBAPI requests are concurrent, thethread pool that IIS processes the request is accounted for by this resource-consuming operation (such as accessing the database, or requesting other services), and theIIS thread pool availability is reduced when the operation completes the front-thread.
Then the individual is very lazy, do not want to write those traditional asynchronous implementation, inadvertently see colleague's recommendation, very surprised, this Nima asynchronous implementation is too simple, so I can't help but want to share!!
Examples are as follows:
1. Original code, synchronous processing
[HttpGet]
Public Hotellistresponse List (...
{
...
Method has a lot of data acquisition and processing operations, not all of them posted
result = HotelBusiness.Instance.QueryHotelList (parameter); //Blocking IIS Threads
return result;
}
2. Add only a few keywords, it becomes asynchronous processing
[HttpGet]
Public Async task<hotellistresponse> List (...
{
...
Return await Task.Factory.StartNew (() = HotelBusiness.Instance.QueryHotelList (parameter)); Operation of resource consumption, moved to asynchronous
}
3. After the modified code runs, the current IIS thread ID is 9 When a request comes in
4. An operation that consumes resources is run on another thread and does not occupy the thread that IIS processes the request
about using Async and Await of Asynchronous Programming
This is mainly the use of async and await keyword, is the new features of. NET 4.5, please refer to
Asynchronous programming with Async and Await (C # and Visual Basic)
Url:https://msdn.microsoft.com/en-us/library/hh191443.aspx
Url:https://msdn.microsoft.com/zh-cn/library/hh191443.aspx (Chinese translation version)
I also opened the blog, is the first time to write a blog, if there are shortcomings, you are welcome to criticize the great God, if there are different insights, may wish to write below, thank you!
Asynchronous programming using Async and Await (C #)