Edit

Share via


LogicalMethodInfo.BeginInvoke(Object, Object[], AsyncCallback, Object) Method

Definition

Begins an asynchronous invocation of the method represented by this LogicalMethodInfo.

public IAsyncResult BeginInvoke(object target, object[] values, AsyncCallback callback, object asyncState);

Parameters

target
Object

The instance of the Object on which to invoke the method on.

values
Object[]

An argument list for the invoked method. This is an array of objects with the same number, order, and type as the parameters of the method. If the method does not require any parameters, values should be null.

callback
AsyncCallback

The delegate to call when the asynchronous invoke is complete. If callback is null, the delegate is not called.

asyncState
Object

State information that is passed on to the delegate.

Returns

An IAsyncResult which is passed to EndInvoke(Object, IAsyncResult) to obtain the return values from the remote method call.

Exceptions

The target parameter is null.

The number, type, and order of parameters in values do not match the signature of the invoked method.

The caller does not have permission to invoke the method.

Examples

public static void Main()
{
   // Get the type information.
   // Note: The MyMath class is a proxy class generated by the Wsdl.exe
   // utility for the Math Web service. This class can also be found in
   // the SoapHttpClientProtocol class example.
   Type myType = typeof(MyMath.MyMath);

   // Get the method info.
   MethodInfo myBeginMethod = myType.GetMethod("BeginAdd");
   MethodInfo myEndMethod = myType.GetMethod("EndAdd");

   // Create an instance of the LogicalMethodInfo class.
   LogicalMethodInfo myLogicalMethodInfo =
      (LogicalMethodInfo.Create(new MethodInfo[] {myBeginMethod,myEndMethod},
      LogicalMethodTypes.Async))[0];

   // Get an instance of the proxy class.
   MyMath.MyMath myMathService = new MyMath.MyMath();

   // Call the MyEndIntimationMethod method to intimate the end of
   // the asynchronous call.
   AsyncCallback myAsyncCallback = new AsyncCallback(MyEndIntimationMethod);

   // Begin to invoke the Add method.
   IAsyncResult myAsyncResult = myLogicalMethodInfo.BeginInvoke(
      myMathService,new object[]{10,10},myAsyncCallback,null);

   // Wait until invoke is complete.
   myAsyncResult.AsyncWaitHandle.WaitOne();

   // Get the result.
   object[] myReturnValue;
   myReturnValue = myLogicalMethodInfo.EndInvoke(myMathService,myAsyncResult);

   Console.WriteLine("Sum of 10 and 10 is " + myReturnValue[0]);
}

// This method will be called at the end of the asynchronous call.
static void MyEndIntimationMethod(IAsyncResult Result)
{
   Console.WriteLine("Asynchronous call on Add method finished.");
}

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1