(go) How C # uses asynchronous programming

Source: Internet
Author: User

How to use async, which is handled with a delegate, can execute this method asynchronously if the delegate object has only one method in the invocation list. The delegate class has two methods, called BeginInvoke and EndInvoke, which are used for asynchronous execution.

There are three modes of asynchrony

    1. Wait mode, after the asynchronous method has been initiated and some other processing has been done, the original thread is interrupted and waits for the asynchronous method to complete before continuing.
    2. Polling mode, the original thread periodically checks whether the originating thread is complete, and if not, can continue to do something else.
    3. Callback mode, where the original thread is executing without waiting or checking whether the originating thread is complete. After the reference method in the originating thread completes, the originating thread invokes the callback method, which is used by the callback method to handle the structure of the Async method before calling EndInvoke.

Before you learn asynchronous programming, look at the BeginInvoke and EndInvoke methods.

BeginInvoke method

    1. When BeginInvoke is called, the arguments in the argument list consist of the following:

1) The arguments required by the reference method.

2) Two additional parameters--callback parameter and state parameter.

    1. BeginInvoke gets a thread from the thread pool and runs the reference method at the beginning of the new thread.
    2. BeginInvoke returns an object that implements the IAsyncResult interface to the calling thread. This interface reference contains the current state of the Async method, and the original thread can then continue execution.

EndInvoke method

    1. It takes a reference to the IAsyncResult object returned by the BeginInvoke method and finds its associated thread.
    2. If thread pool threads have exited, EndInvoke do the following things.

1) It cleans up the state of the exit thread and frees its resources.

2) It finds the value returned by the reference method and takes its value as the return value.

    1. If the thread pool's threads are still running when EndInvoke is invoked, the calling thread stops and waits until the cleanup is complete and the value is returned. Because EndInvoke is cleaning up for open threads, you must make sure that EndInvoke is called on every BeginInvoke.
    2. If an async method fires an exception, an exception is thrown when the EndInvoke is called.

Wait mode

In this mode, the original thread initiates the invocation of an asynchronous method, does some other processing, and then stops and waits until the thread that is opened ends. Such as

This code produces the following output.

Now that we have seen the simplest form of BeginInvoke and Endinoke, we can learn more about IAsyncResult, which is a necessary part of using these methods.

BeginInvoke returns a reference to an IAsyncResult interface (internally, an object of the AsyncResult class). The AsyncResult class shows the state of the Async method. Such as:

    1. When we invoke the BeginInvoke method of the delegate object, the system creates an object of the AsyncResult class. However, it does not return a reference to the object of the class, but instead returns a reference to the IAsyncResult interface contained in the object.
    2. The AsyncResult object contains a property called AsyncDelegate, which returns a reference to a delegate that is invoked to open an async method. But this property is part of the class object but part of the interface.
    3. The IsCompleted property returns a Boolean value that indicates whether the Async method is complete.
    4. The AsyncState property returns a reference to an object that is used as the state parameter when the BeginInvoke method is called. It returns a reference to type object, which is explained later:

Polling mode

In polling mode, the original thread initiates the invocation of the Async method, does some other processing, and then uses the IsCompleted property of the IAsyncResult object to periodically check whether the open thread is complete. If the Async method is complete, the original thread calls EndInvoke and continues. Otherwise, it does some other processing and then checks again later. Such as:

This code produces the following output.

Callback mode

In the previous wait mode with polling mode, the initial thread continues its own control flow until it knows the open thread is complete. Then it gets the result and continues.

The difference in callback mode is that once the initial thread initiates an asynchronous method, it itself takes care of itself and no longer considers synchronization. When the asynchronous method call ends, the system calls a user-defined method to process the end and invokes the delegate's EndInvoke method. This user-defined method is called a callback method or callback.

The last two additional parameters in the BeginInvoke parameter list are used by the callback method:

1) The first parameter, the callback parameter, is the name of the callback method.

2) The second parameter, the state parameter, can be null or an object data to be passed to the callback method. We can get this object by using the AsyncState property of the IAsyncResult parameter. Parameter type is Object

    1. The signature and return type of the callback method must match the form described by the AsyncCallback delegate type. It takes a method to accept a IAsyncResult as a parameter and the return type is void. As shown below:

Void AsyncCallback (IAsyncResult IAR)

    1. Within the callback method, our code should call the delegate's EndInvoke method to handle the output value after the asynchronous method executes. To invoke the delegate's EndInvoke method, we definitely need a reference to the delegate object, which is in the initial thread and not in the open thread. If we do not use the BeginInvoke state parameter for other purposes, you can use it to send a reference to the delegate to the callback method. Otherwise, we can extract the delegate's reference from the IAsyncResult object that is sent to the method as a parameter.

1) Only one parameter to the callback method is a reference to the IAsyncResult interface of the asynchronous method that just ended, remember that the IAsyncResult interface object is inside the AsyncResult class object.

2) Although the IAsyncResult interface does not have a reference to a delegate object, the AsyncResult class object that contains it has a reference to the delegate object.

3) with a reference to the class object, we can now invoke the AsyncDelegate property of the class object and convert it to the appropriate delegate type. This gives you a delegate reference, which we can use to invoke EndInvoke.

As shown in the following code:

This code produces the following output.

Then the above asynchronous content has been basically explained.

Description: Asynchronous programming is in. In Net4.0, there is a better way to deal with it, simplifying the complexity of programming and working with the task class (in the System.Threading.Tasks namespace), but the principle is the same.

(go) How C # uses asynchronous programming

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.