The benefits and methods of C asynchronous invocation are Shared

  • 2020-05-07 20:15:59
  • OfStack

Asynchronous methods solve these problems very well. If a method is executed asynchronously, the program immediately creates a new thread to run your method, and the main thread including the interface will not die. How asynchrony starts, that makes sense, and now we're talking about how to end this new thread that's coming out of asynchrony.
First of all, asynchronous out of the new thread, must be recycled, do not recycle is a waste of resources shameful behavior,.NET is not allowed, so you don't want to game, as the saying goes, please god is easy to send god is this truth. Below you can easily think of, recycling is divided into two kinds of circumstances: active recycling and passive recovery (of course, this is my own understanding, Microsoft is not to say so), recycling is actively, you to monitor the thread, and wait for, when an asynchronous method is completed, the asynchronous threads recycling, focus back to the main thread, in fact is the last article "C # asynchronous preliminary" of that kind of situation, BeginInvoke EndInvoke afterwards, if at the time of EndInvoke, the asynchronous thread did not complete the operation, so the whole program, including the main thread, is blocked again, interface of "death" would happen again. To solve this problem, use a "passive recycle" approach, one of the important approaches is "asynchronous callback". The core is 2: A, with the callback function (CallBackMethod in this case), which is automatically called when the asynchronous ends. B, instead of waiting for the asynchronous end manually in the main thread, where EndInvoke was called in the main thread in the two examples above. This method is called in the callback function EndInvoke. Asynchronous callback about process is as follows: first of all start asynchronous, at the end of the boot parameters with asynchronous execution method, then the asynchronous thread need not tube, and finally finished the work when the asynchronous thread himself, can automatically perform the method in the launch parameters, it is really worry, code to write, however, is very complex. Here is the search code:
 
// First, be prepared to do asynchronous methods (asynchronous, preferably not multi-threaded)  
privatestringMethodName(intNum,outintNum2) 
{ 
Num2=Num; 
return"HelloWorld"; 
} 
// Program at the end of  
// Method (callback method) to execute when completed asynchronously, which method can only have IAsyncResult1 But this parameter is almost omnipotent and can be passed object 
privatevoidCallBackMethod(IAsyncResultar) 
{ 
// From asynchronous state ar.AsyncState Gets the delegate object  
DelegateNamedn=(DelegateName)ar.AsyncState; 
// Output parameters  
inti; 
//1 Need to EndInvoke Or you'll come to a bad end  
stringr=dn.EndInvoke(outi,ar); 
MessageBox.Show(" Asynchronous finish! i The value is "i.ToString()",r The value is "r); 
} 
// Defines a delegate that is signed with the method  
privatedelegatestringDelegateName(intNum,outintNum2); 
// Program entrance  
privatevoidRun() 
{ 
// Instantiate the delegate and assign it initially  
DelegateNamedn=newDelegateName(MethodName); 
// Output parameters  
inti; 
// Instantiate the callback method  
// the AsyncCallback as Delegate You get the idea, actually AsyncCallback is 1 special Delegate , like Event like  
AsyncCallbackacb=newAsyncCallback(CallBackMethod); 
// Asynchronous start  
// If the parameter acb Switch to null There is no callback method  
// The last 1 A parameter dn Can be replaced by any object that can be retrieved from the parameter by the callback method and written as null Can also. parameter dn Equivalent to that thread ID , if you have multiple asynchronous threads, you can have both null But definitely not 1 The sameness cannot be the same 1 a object Otherwise exception  
IAsyncResultiar=dn.BeginInvoke(1,outi,acb,dn); 
// Go do something else  
// .....................  
} 
// The end result should be: i=1 . r="HelloWorld" 
// In addition, if possible, you can define the delegate without too much decoration:  
///<summary> 
/// Define the entrusted  
///</summary> 
///<returns></returns> 
publicdelegateboolAsyncdelegate(); 
///<summary> 
///Callbackmethodmusthavethesamesignatureasthe 
///AsyncCallbackdelegate 
///</summary> 
///<paramname="ar"></param> 
privatevoidCallbackMethod(IAsyncResultar) 
{ 
//Retrievethedelegate. 
Asyncdelegatedlgt=(Asyncdelegate)ar.AsyncState; 
//CallEndInvoketoretrievetheresults. 
dlgt.EndInvoke(ar); 
} 
// Called in other methods:  
// Asynchronous execution  
// Specify delegate method  
Asyncdelegateisgt=newAsyncdelegate(icpInfo.Insert); 
IAsyncResultar=isgt.BeginInvoke(newAsyncCallback(CallbackMethod),isgt); 

Related articles: