C # Async await

Source: Internet
Author: User

1.async await
private void Form2_load (object sender, EventArgs e)        {            Console.WriteLine ("Main:" + THREAD.CURRENTTHREAD.MANAGEDTHREADID);            Dosth ();  No await does not wait for dosth execution to complete directly eject Form2_load            MessageBox.Show ("Form2_load");                  }        Async Task Dosth ()        {            Do1 ();//No await does not wait for Do1 to finish (pop dosth executed after 3 pops do1 after 5s) equivalent to concurrency, add await will wait Do1 The subsequent execution of the line is equivalent to blocking            await task.delay (in);   Here can not use thread.sleep instead of the reason see the end of the red text (if not the thread Thread.Sleep will block the main thread)            Console.WriteLine ("Dosth:" + THREAD.CURRENTTHREAD.MANAGEDTHREADID);            MessageBox.Show ("Dosth executed after 3s");        }        Async Task Do1 ()        {            await task.delay ();            Console.WriteLine ("Do1:" +thread.currentthread.managedthreadid);            MessageBox.Show ("Do1 after 5s");        }

  

Thread ID Output 3 is the same reason (http://www.cnblogs.com/mushroom/p/4575417.html):

There is talk that Async does not have to use the thread, there is a need to say, from a single aspect are right, also is wrong. The above source code is simple analysis, the specific async internal will involve thread context switch, thread reuse, scheduling and so on. Want to deepen the classmate can study under Executioncontextswitcher, SECURITYCONTEXT.RESTORECURRENTWI, ExecutionContext these several east.

In fact, the details of the physical thread can not be too concerned about, know its "main thread a logic---" Asynchronous task thread B logic, the main thread C logic "the basic principle can be. In addition, Async will also be wired overhead, so you should be reasonable to use the business scenario.

From:http://www.cnblogs.com/xuejianxiyang/p/7089280.html

C # Async await

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.