Asp.net asynchronous programming and asp.net asynchronous programming

Source: Internet
Author: User

Asp.net asynchronous programming and asp.net asynchronous programming

Finally, I graduated and successfully entered an expected travel Internet company. 27. I spent more than a month without writing code.

Preface (learning with questions) I. Based on the Code and execution results, I will explore the implementation process of asynchronous programming.

*Question 1: WILL await keep the current thread waiting?

*Question 2: Do I have to wait for the await data to return to the waiting thread for further execution?

*Question 3: is the thread that executes the next statement to await the thread that executes await?

Ii. asynchronous programming async and await application significance and applicable scenarios.

*Question 1: Can asynchronous programming make programs faster?

*Question 2: Is it useful for asynchronous programming to execute time-consuming computing?

*Question 3: What is the significance of asynchronous programming in the same execution sequence and synchronization?

Confusing 1 (initial results) test code
Class Program {static void Main (string [] args) {FirsTask (); Console. readLine ();} public static async Task <string> FirsTask () {Console. writeLine ("1. the current Thread is: "+ Thread. currentThread. managedThreadId); var result = await SecondTask (); Console. writeLine ("6. the current Thread is: "+ Thread. currentThread. managedThreadId); return result;} public static async Task <string> SecondTask () {using (var client = new HttpClient () {Console. writeLine ("2. the current Thread is: "+ Thread. currentThread. managedThreadId); var result = await client. getAsync (" http://stackoverflow.com/questions/37991851/jenkins-configure-page-not-loading-version1-651-3-chrome-browser "); Await ThirdTask (); Console. writeLine ("5. the current Thread is: "+ Thread. currentThread. managedThreadId); return await result. content. readAsStringAsync () ;}} public static async Task <string> ThirdTask () {using (var client = new HttpClient () {Console. writeLine ("3. the current Thread is: "+ Thread. currentThread. managedThreadId); var result = await client. getAsync (" http://stackoverflow.com/questions/37993657/managing-outgoing-calls-settings-in-skype-for-business-client-using-remote-power "); Console. WriteLine (" 4. The current Thread is: "+ Thread. CurrentThread. ManagedThreadId); return await result. Content. ReadAsStringAsync ();}}}
The output result shows the first three problems: Output 1, 6, 2, 5, 3, and 4 are usually not the same thread, Not necessarily the same thread. It turns out that await Does not let the current thread waitThe await result is then executed by the waiting thread. However, when await is encountered, the current thread will be ReleaseTo the thread pool. After await returns the result Call any idle thread for downward execution. Confused 2 (meaning and purpose) because the current thread will be Release to thread pool, NoWaiting in time Occupy thread Resources. Facts also prove that the execution sequence and synchronization code of our current asynchronous code Execution sequenceIn this case, the thread is released and can be reused. This is important because the number of threads in the thread pool in iis is limited. If one hundred requests can be concurrently, so one hundred requests cause one hundred threads to be waiting. However, in the asynchronous process, the thread is released, and the blocking wait time is gone. Accommodate more requests. The program needs to run down and wait until the await result, so synchronous and asynchronous The execution time is the same.Will not reduce the execution time because of Asynchronization.

Asynchronous programmingEspecially suitable for network operationsBecause the network operation can only wait for the request to be completed after initiating the request, the thread that runs this operation can be released and will be in the thread pool. After the network operation is completed, then, a random thread is taken from the thread pool to continue execution.

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.