The process and interface won't 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 can 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, which is actually the case in the previous article "C # asynchronous preliminary, beginInvoke is followed by EndInvoke. If the asynchronous thread does not complete the operation during EndInvoke, the entire program, including the main thread, is blocked again, the interface is "dead. To solve this problem, we should adopt the "passive reclaim" method. An important method is "Asynchronous callback ". There are two core components: A. Use A callback function (CallBackMethod in this example) to automatically call this callback function after The Asynchronization ends. B. Do not manually wait for the asynchronous end in the main thread. In the above 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, but it is very complicated to write the code. The code for souzang is as follows:
Copy codeThe Code 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 );