Several asynchronous operations

Source: Internet
Author: User
In fact, this is also a question asked during the interview: (I am here to commemorate it. Note: It is just a simple list. For detailed principles and analysis, see the relevant chapter of the third edition of CLR via C) 1. Use the thread pool to initiate asynchronous operations
 
Using system; using system. threading; namespace asynchronous {class program {static void main (string [] ARGs) {console. writeline ("main thread: Prepare to initiate a series of asynchronous operations... "); threadpool. queueuserworkitem (computeboundop, 5); threadpool. queueuserworkitem (computeboundop, 7); console. writeline ("main thread: do other things... "); thread. sleep (1, 1000); console. writeline ("press enter to exit... "); console. readline ();} Private Static void computeboundop (Object O) {console. writeline ("Asynchronous Operation callback: State = {0}", O); thread. sleep (1000 );}}}
Result: or 2. Use the task in threading. Tasks.
Using system; using system. threading; using system. threading. tasks; namespace asynchronous {class program {static void main (string [] ARGs) {console. writeline ("main thread: Prepare to initiate a series of asynchronous operations... "); task T = new task (computeboundop, 5); T. start (); console. writeline ("main thread: do other things... "); thread. sleep (1, 1000); console. writeline ("press enter to exit... "); console. readline ();} Private Static void computeboundop (Object O) {console. writeline ("Asynchronous Operation callback: State = {0}", O); thread. sleep (1000 );}}}
3. Use System. Threading. Timer
Using system; using system. threading; namespace asynchronous {class program {static void main (string [] ARGs) {console. writeline ("main thread: Prepare to initiate a series of asynchronous operations... "); timer T = new timer (computeboundop, 5, 50, 0); console. writeline ("main thread: do other things... "); thread. sleep (1, 1000); console. writeline ("press enter to exit... "); console. readline ();} Private Static void computeboundop (Object O) {console. writeline ("Asynchronous Operation callback: State = {0}", O); thread. sleep (1000 );}}}
4. Use the beginxxx method in APM (asynchronous programming model)
Using system; using system. threading; namespace asynchronous {class program {delegate void mydelegate (Object O); static void main (string [] ARGs) {console. writeline ("main thread: Prepare to initiate a series of asynchronous operations... "); mydelegate = computeboundop; mydelegate. begininvoke (null, computeboundopcallback, 5); console. writeline ("main thread: do other things... "); thread. sleep (1, 5000); console. writeline ("press enter to exit... "); console. readline ();} Private Static void computeboundop (Object O) {thread. sleep (1000); // simulate asynchronous operations in some time-consuming operations} Private Static void computeboundopcallback (iasyncresult AR) {console. writeline ("Asynchronous Operation callback: {0}", ar. asyncstate );}}}
To be continued...

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.