Asynchronous WebService call method and instanceCodeAs follows:
Main method (WebService call method ):
Arraylist al = New Arraylist ();
Localhost. testservice TS = New Testwebpro. localhost. testservice (); // This is the proxy class of WebService locally.
System. Threading. manualresetevent MRE = New System. Threading. manualresetevent ( False );
Al. Add (TS );
Al. Add (MRE );
// Execute asynchronous call. localhost. testenum. Bus is a custom enumeration and is used as a parameter of the call method.
System. iasyncresult ar = TS. begintestmethod (localhost. testenum. Bus, New System. asynccallback ( This . Endcall), Al );
// . Execute other operations
Mre. waitone (); // Wait until the call is completed
// . Continue other operations
Callback method endcall (the method executed after the call is complete ):
Public Void Endcall (system. iasyncresult AR)
{
Try
{
Arraylist al = (Arraylist) Ar. asyncstate;
Localhost. testservice TS = (Localhost. testservice) al [ 0 ];
System. Threading. manualresetevent MRE = (System. Threading. manualresetevent) al [ 1 ];
String S = TS. endtestmethod (AR ); // Obtain call results
Mre. Set (); // The notification master method can continue execution
}
Catch (Exception ex)
{
ThrowEx;
}
}
If the primary method and callback method are in the same class, TS and MRE can be declared as private global variables instead of being passed to the callback function through iasyncresult. asyncstate.