C # Multi-thread asynchronous callback

Source: Internet
Author: User

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

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.