Benefits and methods of C # asynchronous invocation sharing _c# Tutorials

Source: Internet
Author: User
Asynchronous methods are a good solution to these problems, the asynchronous execution of a method, the program immediately opened a new thread to run your method, the main thread, 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, asynchronous new threads 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 is executed when the asynchronous completes, this method can only have IAsyncResult one parameter, but the parameter is almost omnipotent, can pass object
Privatevoidcallbackmethod (Iasyncresultar)
{
From the asynchronous state ar. AsyncState, get the delegate object
delegatenamedn= (delegatename) ar. asyncstate;
Output parameters
Inti
You must EndInvoke, or you will end up miserable.
Stringr=dn. EndInvoke (Outi,ar);
MessageBox.Show ("Done asynchronously!") The value of I is "i.tostring ()" and the value of R is "R";
}
To define a delegate that has the same signature as a method
Privatedelegatestringdelegatename (INTNUM,OUTINTNUM2);
Program entry
Privatevoidrun ()
{
Instantiating a delegate and assigning it to the initial value
Delegatenamedn=newdelegatename (methodname);
Output parameters
Inti
Instantiating a callback method
Think of AsyncCallback as delegate you know, actually AsyncCallback is a special delegate, like an event.
Asynccallbackacb=newasynccallback (Callbackmethod);
Start asynchronously
If the parameter ACB to NULL means 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:
<summary>
Defining delegates
</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
Specifying delegate methods
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.