The benefits and methods of C # asynchronous invocation sharing

Source: Internet
Author: User

process, including the interface, will not die. How to start asynchronous, well understand, now we are talking about how to end this asynchronous out of the new thread.  


first, the new thread that comes out asynchronously must be recycled, and not recycled is a shameful act of wasting resources. NET is also not allowed, so you do not want to loopholes, as the saying goes, please God easy to send God difficult, is this truth. Below you can easily think that recycling is divided into 2 kinds of situations: active recovery and passive recycling (of course, this is my own understanding, Microsoft does not say so), active recycling is that you go to monitor the thread, and wait, when the asynchronous method is completed, the asynchronous thread recycling, focus back to the main thread, is actually the last article " C # Asynchronous preliminary in that case, after BeginInvoke and EndInvoke, if in EndInvoke, the asynchronous thread does not complete the operation, then the entire program, including the main thread, and in the blocking, will appear interface "dead" situation. One important way to solve this problem is to use the passive recycle method, which is the "asynchronous callback." The core has two: A, with a callback function (Callbackmethod in this case), and automatically call this callback function after the asynchronous end. B, instead of manually waiting for the asynchronous end in the main thread, as in the last two cases, call EndInvoke in the main thread. In this way, you call the EndInvoke in the callback function. The approximate process for asynchronous callbacks is this: Start the asynchronous, start the parameter plus the asynchronous end of the method to execute, and then this asynchronous thread does not have to, and finally when the asynchronous thread completes its own work, it automatically executes the startup parameters of the method, it is really very worry, but the code is written, it is very complicated. Here is the search code:


Copy Code code as follows:


//First Ready, asynchronous method (asynchronous, preferably not multiple threads)


privatestringmethodname (intnum,outintnum2)


{  


Num2=num;  


return "HelloWorld";  


}  


//Program endpoint

The method (callback method) that executes when
//asynchronously completes, this method can only have IAsyncResult one parameter, but this parameter is almost omnipotent, can pass <span class="Apple-converted-space"></span> object


Privatevoidcallbackmethod (Iasyncresultar)


{  


//From asynchronous State ar. AsyncState, gets the delegate object


delegatenamedn= (delegatename) ar. asyncstate;  


//Output parameters 


Inti;  


//Must be EndInvoke, or your end will be miserable


Stringr=dn. EndInvoke (Outi,ar);  


MessageBox.Show ("Done asynchronously!") The value of I is "i.tostring ()" and the value of R is "R";  


}  


//Definition and method with the signature of the <span class="Apple-converted-space"></span> delegate


Privatedelegatestringdelegatename (INTNUM,OUTINTNUM2);  


//program entrance <span class="Apple-converted-space"></span>


Privatevoidrun ()


{  


//Instantiate a delegate and assign a value first 


Delegatenamedn=newdelegatename (methodname);  


//output parameter <span class="Apple-converted-space"></span>


Inti;  


//Instantiation callback method


//AsyncCallback as Delegate you know, in fact, AsyncCallback is a special delegate, like the event


Asynccallbackacb=newasynccallback (Callbackmethod);  


//Asynchronous start


///If the parameter ACB replaced by null means there is <span class="Apple-converted-space"></span> No callback method


//The last parameter DN can be replaced with any object that can be retrieved from the parameter by the callback method and written as null. The parameter DN corresponds to the ID of the thread, and if there are more than one asynchronous thread, it can all be null, but absolutely not the same, not the same object, otherwise the exception 


Iasyncresultiar=dn. BeginInvoke (1,OUTI,ACB,DN);  


//To do something else


///...
.

}  


//The final result should be: i=1,r= "HelloWorld"


//In addition, if you can, you can choose to define the delegate without excessive modification <span class="Apple-converted-space"></span> :


///&lt;summary&gt;


///define delegate


///&lt;/summary&gt;


///&lt;returns&gt;&lt;/returns&gt;


Publicdelegateboolasyncdelegate ();  


///&lt;summary&gt;


///callbackmethodmusthavethesamesignatureasthe


///asynccallbackdelegate


///&lt;/summary&gt;


///&lt;paramname= "ar" &gt;&lt;/param&gt;


Privatevoidcallbackmethod (Iasyncresultar)


{  


//retrievethedelegate.  


asyncdelegatedlgt= (asyncdelegate) ar. asyncstate;  


//callendinvoketoretrievetheresults.  


dlgt. EndInvoke (AR);  


}  


//Other method call:


//Asynchronous Execution


//Designation Delegate method


asyncdelegateisgt=newasyncdelegate (Icpinfo.insert);  


Iasyncresultar=isgt. BeginInvoke (Newasynccallback (Callbackmethod), ISGT);

Related Article

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.