usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Remoting.Messaging;usingSystem.Text;usingSystem.Threading;namespaceyibu{ Public Delegate intAddHandler (intAintb); Public classCADD { Public Static intADD (intAintb) {Console.WriteLine ("Start calculation ..."); Console.WriteLine ("Current thread ID:"+Thread.CurrentThread.ManagedThreadId); Thread.Sleep (4000); Console.WriteLine ("Calculation Complete"); returnA +b; } } classProgram {voidCleanupintresult) {Console.WriteLine ("The caller continues to work"); Console.WriteLine (result); Console.readkey (); } Static voidSync_call () {Console.WriteLine ("sync_call_test"); AddHandler Handler=NewAddHandler (Cadd.add); intresult = handler. Invoke (1,2); Console.WriteLine ("The caller continues to work"); Console.WriteLine (result); Console.readkey (); } Static voidAsync_call () {Console.WriteLine ("async_call_test"); AddHandler Handler=NewAddHandler (Cadd.add); IAsyncResult result= handler. BeginInvoke (1,2,NULL,NULL); Console.WriteLine ("The caller continues to work"); Console.WriteLine (handler. EndInvoke (result)); Console.readkey (); } Static voidAsync_callback () {Console.WriteLine ("async_callback_test"); AddHandler Handler=NewAddHandler (Cadd.add); IAsyncResult result= handler. BeginInvoke (1,2,NewAsyncCallback (Mycall),"Asyncstate:ok"); Console.WriteLine ("The caller continues to work"); //Console.readkey (); } Static voidMain () {Console.WriteLine ("main thread ID:"+Thread.CurrentThread.ManagedThreadId); Async_callback (); Console.WriteLine ("Happy Coding,yes"); Thread.Sleep (44000); Console.WriteLine ("Continue Happy Coding"); Thread.Sleep ( -); Console.readkey (); } Static voidMycall (IAsyncResult result) {Console.WriteLine ("where am I?"); Console.WriteLine ("Current thread ID:"+Thread.CurrentThread.ManagedThreadId); AddHandler Handler=( AddHandler) ((AsyncResult) result). AsyncDelegate; Console.WriteLine (handler. EndInvoke (result)); Console.WriteLine (Result. asyncstate); } }}
The main function in the code uses an asynchronous callback, in order to illustrate its superiority, the Code provides a
There are two other ways to make comparisons:
1. Synchronous invocation, code in the Sync_call function,
This is actually called by this thread, and calling a function is no different.
2. Asynchronous invocation
In the Async_call function, the end of handler is called. After the BeginInvoke, the main thread will continue to execute down,
But in handler. EndInvoke, if the task is not completed, it will still be blocked here.
Note The core code that uses the asynchronous callback:
IAsyncResult result = handler. BeginInvoke (1, 2, new AsyncCallback (Mycall), "Asyncstate:ok");
The third parameter registers the callback function, and the third one passes an object.
The callback function Mycall executes in the task thread,
AddHandler handler = (AddHandler) ((AsyncResult) result). AsyncDelegate; To get the delegate object, and then
Handler. The EndInvoke (result) return value.
The delegate object that gets the main thread in the callback function can also use the following methods:
When BeginInvoke, the delegate object is passed to the fourth parameter,
Use result in the callback function. Asynstate and then turn into (AddHandler)
AddHandler handler = (AddHandler) (result. asyncstate);
C # Multi-thread asynchronous callback