We need to clarify why asynchronous callback is required? As we all know, normal methods run in a single thread. Large operations (such as reading large files, operating databases in large batches, and network transmission) in the middle will lead to method blocking, as shown on the interface, Program Card or die, the interface element does not move, no response. The asynchronous method solves these problems. When a method is executed asynchronously, the program immediately opens up a new thread to run your method. The main thread, including the interface, will not die. Now we are talking about how to end this asynchronous new thread.
First, the asynchronous new thread must be recycled. It is a shameful waste of resources ,. net is also not allowed, so you do not want to explore the loopholes, as the saying goes, please be easy to god, it is this truth. Next, you can easily think of two types of recovery: active recovery and passive recovery (of course, this is my understanding, but Microsoft does not say so). Active recovery is, you need to monitor the thread and wait. When the Asynchronous Method is complete, the asynchronous thread will be recycled, and the focus will be returned to the main thread. This is actually the first article.ArticleIn C # asynchronous preliminary, endinvoke is followed by begininvoke. If the asynchronous thread does not complete the operation at endinvoke, the entire program, including the main thread, it is blocked again, and the interface will be "dead. To solve this problem, we should adopt the "passive reclaim" method. An important method is "Asynchronous callback ". Core: A, Use the callback function (callbackmethod in this example) ). This callback function is automatically called after the asynchronous process ends. B, In the preceding two examples, endinvoke is called in the main thread. This method calls endinvoke In the callback function. The general process of asynchronous callback is as follows: first, start Asynchronization. Then, the startup parameter is added with the method executed at the end of Asynchronization. Then, this asynchronous thread will be ignored, finally, when the asynchronous thread completes the work, it will automatically execute the method in the startup parameter, which is indeed very worry-free,CodeWriting is complicated. The code for souzang is as follows:
-
- // First, prepare the Asynchronous Method (asynchronous, preferably not multithreading)
-
- Privatestringmethodname (intnum, outintnum2)
-
- {
-
- Num2 = num;
-
- Return "helloworld ";
-
- }
-
- // Program endpoint
-
- // The method (callback method) to be executed when asynchronous completion is completed. This method can only have one iasyncresult parameter, but this parameter is almost omnipotent and can be passed to the object.
-
- Privatevoidcallbackmethod (iasyncresultar)
-
- {
-
- // Obtain the delegate object from the asynchronous ar. asyncstate.
-
- Delegatenamedn = (delegatename) Ar. asyncstate;
-
- // Output parameters
- Inti;
-
- // Be sure to use endinvoke. Otherwise, your end will be miserable.
-
- Stringr = DN. endinvoke (Outi, AR );
-
- MessageBox. Show ("Asynchronous completion! The value of I is "I. tostring ()", and the value of R is "R );
-
- }
-
- // Define the delegate with the same signature as the method
-
- Privatedelegatestringdelegatename (intnum, outintnum2 );
-
- // Program entry
-
- Privatevoidrun ()
-
- {
-
- // Instantiate the delegate and assign a value first
-
- Delegatenamedn = newdelegatename (methodname );
-
- // Output parameters
-
- Inti;
-
- // Instantiate the callback Method
-
- // Consider asynccallback as delegate. In fact, asynccallback is a Special Delegate, just like event.
-
- Asynccallbackacb = newasynccallback (callbackmethod );
-
- // Asynchronous Start
-
- // If the value of ACB is null, no callback method is available.
- // The last parameter dn can be replaced with any object. This object can be obtained by the callback method from the parameter and written as null. The parameter dn is equivalent to the thread ID. If multiple asynchronous threads exist, they can all be null, but they cannot be the same. They cannot be the same object. Otherwise, an exception occurs.
-
- Iasyncresultiar = DN. begininvoke (1, Outi, ACB, DN );
-
- // Do other things
-
- //............
-
- }
-
- // The final result should be: I = 1, R = "helloworld"
-
- // In addition, if you can, you can choose not to modify too much when defining the delegate:
-
- /// <Summary>
-
- /// Define the delegate
-
- /// </Summary>
-
- /// <Returns> </returns>
-
- Publicdelegateboolasyncdelegate ();
-
- /// <Summary>
-
- /// Callbackmethodmusthavethesamesignatureasthe
-
- /// Asynccallbackdelegate
-
- /// </Summary>
-
- /// <Paramname = "Ar"> </param>
- Privatevoidcallbackmethod (iasyncresultar)
-
- {
-
- // Retrievethedelegate.
-
- Asyncdelegatedlgt = (asyncdelegate) Ar. asyncstate;
-
- // Callendinvoketoretrievetheresults.
-
- Dlgt. endinvoke (AR );
-
- }
-
- // Call other methods:
-
- // Asynchronous execution
-
- // Specify the delegate Method
-
- Asyncdelegateisgt = newasyncdelegate (icpinfo. insert );
-
- Iasyncresultar = isgt. begininvoke (newasynccallback (callbackmethod), isgt );
To:Http://www.cnblogs.com/xiaoli0414/archive/2007/11/27/974534.html