Before learning Asynchronization, it is necessary to discuss "Why do we need to understand Asynchronization", that is, how to write Program What are the benefits. Now let's talk about synchronization. The so-called synchronization is executed in the program encoding order, and the rough point of the asynchronous statement is not executed in the encoding order, when do we need asynchronous programming? -------- if we encounter a task that is particularly time-consuming, because the following task is not related to the previous task, we can use Asynchronous execution (of course we can also use multiple threads). If you know when to use Asynchronization, let's start learning asynchronous programming.
1. asynchronous programming steps
1. Create a delegate with the same signature as the called Method
Assume that the called method is public void testmethod (string P)
Public Delegate void asyncdelegate (string P );
2. register the called Method to the delegated instance.
Asyncdelegate ad = new asyncdelegate (testmethod );
3. Call the Asynchronous Method starting with begininvoke
Iasyncresult AR = AD. begininvoke (string P, null, null)
4. Continue to perform operations that do not require asynchronous call results
// Perform other operations
5. Check the execution result of the asynchronous call method when the asynchronous call method result is required, and wait for the asynchronous call to complete synchronously.
While (AR. iscompleted = false)
{
Thread. Sleep (10 );
}
Ad. endbegininvoke ();
//Continue operation
II,Instance (attached to the backend)
3. In asynchronous callsUseIasyncresult(Indicating the status of asynchronous operations) andAsynccallback (Callback method executed when asynchronous call is complete)
1,Create a delegate with the same signature as the called Method
Assume that the called method isPublic void testmethod (string P)
Public Delegate void asyncdelegate (string P );
2,Register the called Method to the delegated instance
Asyncdelegate ad = new asyncdelegate (testmethod );
3,Define the callback method executed when the asynchronous call is completed
Public void callbackmethod (iasyncresult AR)
4,BegininvokeStart to call the Asynchronous Method
Iasyncresult AR = AD. begininvoke (string P, new asynccallback (callbackmethod), null)
5, Continue to do some operations that do not require asynchronous call results
//Perform other operations
4. instances (attached to the backend)
V. Summary of "Asynchronous Method signature"
Method signature is the order of parameter types in a method.Begininvoke The Asynchronous Method signature rules are as follows:
- Including allIn Parameters.
- Including allOut Parameters.
- Including allIn/out Parameters.
- Including allByrefParameters.
- SetAsynccallback AndAsyncstate(You can useIasyncresult InterfaceAsyncstate Attribute) as the last two parameters.
- ReturnIasyncresult.
Endinvoke The Asynchronous Method signature rules are as follows:
- Including allIn/out Parameters.
- Including allOut Parameters.
- Including allByrefParameters.
- SetIasyncresult As the last parameter.
- Return the original return type from the original method signature.
Result object(Iasyncresult)Is returned from the start operation and can be used to obtain the status about whether the Asynchronous Start operation has been completed. The result object is passed to the end operation, which returns the final return value of the call. You can provide an optional callback when you start the operation. If a callback is provided, the callback is called after the call is completed.CodeYou can call to end the operation.
Source code:/Files/chinhr/code.rar