C # synchronous and asynchronous understanding, including await and async in 5.0 (learning notes ),

Source: Internet
Author: User

C # synchronous and asynchronous understanding, including await and async in 5.0 (learning notes ),

Previously, I used a synchronization thread, that is, the load event first written into the image, and then processed the database call in it. Later I met some old code in the company's software. When I first added functionality to the old code, I wanted to use the database data to assign some values to the image, then follow the conventional method to find out. I asked my predecessors and found that the solution was asynchronous. The predecessors said that Asynchronization is the most ideal for the user's senses. the synchronous thread is used later because the software is complicated and involves a large amount of data. If some functions cannot obtain the previous data, it is completely impossible to proceed, so it is very cumbersome to use Asynchronous solutions, and then use a synchronous thread.

Synchronous: Method Name (BLL layer, BLL layer method, dto), form the screen and take data: A-A1-B-B1-A2, UI thread is A series, take data is B series, synchronization is to put the data fetch thread in the UI thread's sequential execution process. There is no way to end the UI thread without completing the data.

Asynchronous: Method Name (BLL layer, BLL layer method, Screen Layer Asynchronous Method, dto), form the screen and take data: A-A1----------A2, plug out a new thread B-B1 in parallel with A1 at the ellipsis, then get B1 data before A2, and finally execute the complete process. At first, I thought that Asynchronization was A series of threads and A series of threads. The two did not interfere with each other, and the predecessors told me that this was not the case, before Series A is executed, the final data of Series B is required. Otherwise, the entire thread will not end. So when I use asynchronous mode to bring up a new screen and immediately display the data on the screen, there is a grid data table on the screen to display the data, there will be a sad phenomenon, that is, when the screen pops up, when the data on the grid is displayed, the grid table will shake itself, it is the moment from the blank space to the data filling, and then the whole event is ended. This is completely in line with the process described in Series A and Series B.

As a result, I personally feel that it is better to synchronize images and data ..

Later, when I went back to my summary, I learned about two new keywords in C #5.0, which are also about asynchronous threads, await and async. These two keywords are a pair of twins and are indispensable,

Static void Main (string [] args)
{
DisplayValue ();
Console. WriteLine ("AddValueAsync Start! ");
Console. ReadLine ();
}

Static Task <int> AddValueAsync (int num1, int num2)
{
Return Task. Run () =>
{
Return num1 + num2;
});
}

Static async void DisplayValue ()
{
Console. WriteLine ("Programme Start! ");
Console. writeLine ("*********************************");
Double result = await AddValueAsync (1, 2 );
Console. WriteLine ("Value is:" + result );
Console. WriteLine ("AddValueAsync End! ");
Console. writeLine ("*********************************");
}

The execution result is:

Programme Start!

*********************************

AddValueAsync Start!

Value is: 3

AddValueAsync End!

*********************************

When learning these two keywords at the beginning, I thought the execution result of the program would be:

AddValueAsync Start!

*********************************

Programme Start!

Value is: 3

AddValueAsync End!

*********************************

Finally, I found that the results I imagined at the beginning were completely incorrect. The async modification method would not be asynchronous at all. When I enter an async modification method, when the first method modified by the await keyword is encountered, it will return to the main thread for further execution, and an additional thread will be opened for execution after await. Of course, when you perform single-step debugging in VS, you will find that you will not return to the mian method and execute Console. WriteLine ("AddValueAsync Start! ") The command control window pops up and ends. However, when the result is output, it will still be executed. If AddValueAsync is modified with async, And the await method is nested in it, the thread will be reused. Suppose there are methods 1, 2, 3, and method 1 is thread 1, method 2 or thread 2, but method 3 is not thread 3, but thread 1.

For the first time, I will summarize my study notes. If there is something wrong with it, I would like to extend my warm welcome to point out something wrong. Thank you ~~~

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.