C # improving every day-asynchronous programming mode

Source: Internet
Author: User

Class Program {private delegate int MyDel (int a); public static int MyMenthod (int a) {for (int I = 0; I <1000; I ++) {a ++;} return a;} static void Main (string [] args) {var del = new MyDel (MyMenthod); Console. writeLine ("Before BeginInvoke"); IAsyncResult iar = del. beginInvoke (5, null, null); Console. writeLine ("After BeginInvoke"); Console. writeLine ("Doing stuff"); long result = del. endInvoke (iar); Console. writeLine ("result: {0}", result );}}

 

Class Program {private delegate int MyDel (int a); public static int MyMenthod (int a) {for (int I = 0; I <1000; I ++) {a ++;} Thread. sleep (3000); return a;} static void Main (string [] args) {var del = new MyDel (MyMenthod); IAsyncResult iar = del. beginInvoke (5, null, null); Console. writeLine ("After BeginInvoke"); while (! Iar. isCompleted) {Console. writeLine ("not finished"); Thread. sleep (1, 2000); Console. writeLine ("continue to process other things");} Console. writeLine ("Asynchronous execution completed"); long result = del. endInvoke (iar); Console. writeLine ("result: {0}", result );}}

 

Class Program {private delegate int MyDel (int a); public static int MyMenthod (int a) {for (int I = 0; I <1000; I ++) {a ++;} return a;} public static void CallBack (IAsyncResult iar) {AsyncResult ar = iar as AsyncResult; MyDel del = (MyDel) ar. asyncDelegate; long r = del. endInvoke (iar); Thread. sleep (1, 2000); Console. writeLine ("the result is: {0}", r);} static void Main (string [] args) {var del = new MyDel (MyMenthod); Console. writeLine ("After BeginInvoke"); IAsyncResult iar = del. beginInvoke (5, CallBack, del); Console. writeLine ("Doing More Work In Main"); Thread. sleep (5000 );}}

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.