C # Synchronous calls the role of asynchronous calling asynchronous callbacks multithreading

Source: Internet
Author: User

Synchronous invocation: The Invoke method of the delegate is used for synchronous invocation. A synchronous call can also be called a blocking call, which blocks the current thread and then executes the call, and then continues down after the call is complete.

Asynchronous invocation: A synchronous call blocks a thread and, if it is to invoke a heavy workload (such as a large number of IO operations), may cause the program to stall for a long time, causing bad
User experience, it is necessary to call it asynchronously.
Instead of blocking the thread, the asynchronous call plugs the call into the thread pool, which the program's main thread or UI thread can continue to execute.
The asynchronous invocation of a delegate is implemented by BeginInvoke and EndInvoke.

Asynchronous callback function: When called asynchronously, the main thread does not wait, but runs down directly.
However, the problem persists when the main thread runs to EndInvoke, and if the call is not ended (which is likely to occur), then the threads will still be blocked in order to wait for the result to be called.
The workaround is to use a callback function that automatically calls the callback function when the call ends

Synchronous calling program implementation

Class Program
{
Synchronous invocation
The Invoke method of the delegate is used for synchronous invocation. A synchronous call can also be called a blocking call, which blocks the current thread and then executes the call, and then continues down after the call is complete.
public delegate int AddHandler (int a,int b);//Declaration Delegate
static void Main (string[] args)
{
Console.WriteLine ("******* (synchronous Call) syncinvoketest*******");
AddHandler handler = new AddHandler (ADD);//define delegate, pass in parameter
int result = handler. Invoke (1, 2);//delegate synchronous invocation, the main thread execution to stop at this time, that is, blocking. Call the method of the synchronous call delegate, and execute this method.
Console.WriteLine ("Keep doing other things) do another work ...");//other work refers to the Add method, which adds two numbers.
Console.WriteLine (result);
Console.ReadLine ();
}

private static int Add (int a, int b)//An addition method is a thread
{
Console.WriteLine ("Computing" +a+ "+" +b+ "...");
Thread.Sleep (9000);//simulation method for 9 seconds
Console.WriteLine ("Computing Complete.");
return a + B;
}
}

Asynchronous calling program implementation

Class Program
{
Calling *********** Asynchronously
Instead of blocking the thread, the asynchronous call plugs the call into the thread pool, which the program's main thread or UI thread can continue to execute.
The asynchronous invocation of a delegate is implemented by BeginInvoke and EndInvoke.
public delegate int AddHandler (int a,int b);
static void Main (string[] args)
{
Console.WriteLine ("******** (asynchronous Call) asyncinvoketest******");
AddHandler handler = new AddHandler (ADD);
IAsyncResult result = handler. BeginInvoke (1, 2, NULL, NULL);
Console.WriteLine ("Keep doing other things) do another work ...");
Console.WriteLine (handler. EndInvoke (result));
Console.ReadLine ();
}

private static int Add (int a, int b)
{
Console.WriteLine ("Computing" +a+ "+" +b+ "...");
Thread.Sleep (9000);//simulation method for 9 seconds
Console.WriteLine ("Computing Complete");
return a + B;
}
}

Asynchronous call join callback function program implementation

Class Program
{
Asynchronous Callback ***********
With the callback function, the callback function is automatically invoked at the end of the call, which solves the situation where the thread is still blocked waiting for the result to be called.

Note: BeginInvoke and EndInvoke must be called in pairs. Even if the return value is not required, EndInvoke must still be called, which may cause a memory leak.
public delegate int AddHandler (int a, int b);
static void Main (string[] args)
{
Console.WriteLine ("******** (asynchronous callback Call) asyncinvoketest******");
AddHandler handler = new AddHandler (ADD);
IAsyncResult result = handler. BeginInvoke (1, 2, new AsyncCallback (Addcomplete), "Asycstate:ok");
Console.WriteLine ("Keep doing other things) do another work ...");
Console.WriteLine (handler. EndInvoke (result));//Only one EndInvoke () method can be called per asynchronous operation.
Console.ReadLine ();
}

private static int Add (int a, int b)
{
Console.WriteLine ("Computing" + A + "+" + + B + "....");
Thread.Sleep (3000);//simulation method for 9 seconds
Console.WriteLine ("Computing Complete");
return a + B;
}

static void Addcomplete (IAsyncResult result)//asynchronous callback function
{
AddHandler handler = (AddHandler) ((AsyncResult) result). AsyncDelegate;
Console.WriteLine (handler. EndInvoke (result));
Console.WriteLine (Result. asyncstate);
}
Asynchronous delegate, or you can refer to the following notation:
action<object> action= (obj) =>method (obj);
Action. BeginInvoke (obj,ar=>action. EndInvoke (AR), null);
You can complete an operation in two simple sentences.
}

Multithreading is a good choice to improve the user experience and to solve the form interface's suspended animation status. Synchronization threads are blocking threads and are not helpful for improving the user experience and the interface for suspended animation. Asynchronous threads can improve the performance of the program, and the user experience. Usually said multithreading refers to asynchronous threads.

C # Synchronous calls the role of asynchronous calling asynchronous callbacks multithreading

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.