The company needs to call an external WebService for work. At the same time, it needs to save the outgoing data. As a result, we plan to perform operations step by step as we used, I have never considered the user experience. With my colleagues' guidance, I am aware of the benefits of using asynchronous calls. I just want to save some information I have found so that I can avoid mistakes!
Make it clear that, Why asynchronous callback? 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 new thread generated asynchronously 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. Article In 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. Code As follows:
========================================================== // First, prepare the Asynchronous Method (asynchronous, preferably not multithreading)
Private String Methodname ( Int Num, Out Int Num2)
{
Num2 = Num;
Return " Helloworld " ;
}
// Define the delegate with the same signature as the method
Private Delegate String Delegatename ( Int Num, Out Int Num2 );
// Program entry
Private Void Run ()
{
// Instantiate Delegation
Delegatename DN = New Delegatename (methodname );
Int I;
// Asynchronous Start
Iasyncresult IAR = DN. begininvoke ( 1 , Out I, Null , Null );
// Do other things
// ............
// Finish other things
// Asynchronous termination
String R = DN. endinvoke ( Out I, IAR );
MessageBox. Show (I. tostring () + " " + R );
}
// The final result should be: I = 1, R = "helloworld"
==========================================================
To solve this problem, we should adopt the "passive reclaim" method. An important method is "Asynchronous callback ".
core:
A, use the callback function (in this example, callbackmethod ). This callback function is automatically called after the asynchronous process ends.
B. instead of waiting for asynchronous completion in the main thread, in the above two examples, the 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, add the startup parameter and the method executed at the end of Asynchronization, and then the 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 following code is used to search for hidden objects:
// prepare the Asynchronous Method (asynchronous, preferably not multithreading).
private string methodname (INT num, out int num2)
{< br> num2 = num;
return "helloworld";
}
// program endpoint
// method (callback method) executed when asynchronous completion is completed ), this method can only have one iasyncresult parameter, but this parameter is almost omnipotent. You can pass the object
private void callbackmethod (iasyncresult AR)
{< br> // asynchronous ar. in asyncstate, obtain the delegate object
delegatename DN = (delegatename) ar. asyncstate;
// output parameter
int I;
// be sure to use endinvoke; otherwise, your end is miserable
string r = DN. endinvoke (Out I, 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
Private delegate string delegatename (INT num, out int num2 );
// Program entry
Private void run ()
{
// Instantiate the delegate and assign a value first
Delegatename DN = new delegatename (methodname );
// Output parameters
Int I;
// Instantiate the callback Method
// Consider asynccallback as delegate. In fact, asynccallback is a Special Delegate, just like event.
Asynccallback ACB = new asynccallback (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.
Iasyncresult IAR = DN. begininvoke (1, out I, 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:
///
// define the DeleGate
///
///
Public Delegate bool asyncdelegate ();
//
// callback method must have the same signature as the
// asynccallback DeleGate
///
//
private void callbackmethod (iasyncresult AR)
{< br> // retrieve the delegate.
asyncdelegate dlgt = (asyncdelegate) ar. asyncstate;
// Call endinvoke to retrieve the results.
Dlgt. endinvoke (AR );
}
Call other methods:
// Asynchronous execution
// Specify the delegate Method
Asyncdelegate isgt = new asyncdelegate (icpinfo. insert );
Iasyncresult AR = isgt. begininvoke (New asynccallback (callbackmethod), isgt );
Http://www.cnblogs.com/xiaoli0414/archive/2007/11/27/974534.html