EndInvoke

Retrieve result of asynchronous call initiated by .NET System.Delegate BeginInvoke method

Syntax

result = EndInvoke(asyncResult)
[res0,...,resN] = EndInvoke(res0,...,resN,asyncResult)

Description

result = EndInvoke(asyncResult) retrieves result of asynchronous call initiated by BeginInvoke method.

[res0,...,resN] = EndInvoke(res0,...,resN,asyncResult) for methods with out and/or ref parameters.

Input Arguments

asyncResult

.NET System.IAsyncResult object returned by BeginInvoke.

res0,...,resN

For methods with out and/or ref parameters, results of the asynchronous call. The number of arguments is the sum of:

  • Number of return values (0 or 1).

  • Number of out and ref arguments.

Output Arguments

result

Results of the asynchronous call.

res0,...,resN

For methods with out and/or ref parameters, results of the asynchronous call,

Examples

The following examples show how to call delegates with various input and output arguments. Each example contains:

  1. The C# delegate signature. In order to execute the MATLAB® code, build the delegate code into an assembly named SignatureExamples and load it into MATLAB. For information, see Build a .NET Application for MATLAB Examples.

  2. An example MATLAB function to use with the delegate, which must exist on your path.

  3. The BeginInvoke and EndInvoke signatures MATLAB creates. To display the signatures, create a delegate instance, myDel, and call the methodsview function.

  4. Simple MATLAB example.

This example shows how to use a delegate that has no return value.

  1. C# delegate:

    public delegate void delint(Int32 arg);
  2. MATLAB function to call:

    % Display input argument
    function dispfnc(A)
    % A = number
    ['Input is ' num2str(A)]
    end
    
  3. MATLAB creates the following signatures. For BeginInvoke:

    System.IAsyncResult RetVal	
        BeginInvoke	(
            SignatureExamples.delint this, 
            int32 scalar arg, 
            System.AsyncCallback callback, 
            System.Object object)

    The EndInvoke signature:

        EndInvoke	(
            SignatureExamples.delint this, 
            System.IAsyncResult result)
    
  4. Call dispfnc:

    myDel = SignatureExamples.delint(@dispfnc);
    asyncRes = myDel.BeginInvoke(6, [], []);
    while asyncRes.IsCompleted ~= true
        pause(0.05) % Use pause() to let MATLAB process event
    end
    myDel.EndInvoke(asyncRes)
    
    Input is 6

This example shows how to use a delegate with a return value. The delegate does not have out or ref parameters.

  1. C# delegate:

    public delegate Int32 del2int(Int32 arg1, Int32 arg2);
    
  2. MATLAB function to call:

    % Add input arguments
    function res = addfnc(A, B)
    % A and B are numbers
    res = A + B;
    end
    
  3. MATLAB creates the following signatures. For BeginInvoke:

    System.IAsyncResult RetVal	
        BeginInvoke	(
            SignatureExamples.del2int this, 
            int32 scalar arg1, 
            int32 scalar arg2, 
            System.AsyncCallback callback, 
            System.Object object)
    

    The EndInvoke signature:

    int32 scalar RetVal	
        EndInvoke	(
            SignatureExamples.del2int this, 
            System.IAsyncResult result)
    
  4. Call addfnc.

    myDel = SignatureExamples.del2int(@addfnc);
    asyncRes = myDel.BeginInvoke(6,8,[],[]);
    while asyncRes.IsCompleted ~= true
        pause(0.05) % Use pause() to let MATLAB process event
    end
    result = myDel.EndInvoke(asyncRes)
    
    result =
              14

This example shows how to use a delegate with a ref parameter, refArg, and no return value.

  1. C# delegate:

    public delegate void delrefvoid(ref Double refArg);
    
  2. MATLAB maps the ref argument as both RHS and LHS arguments. MATLAB function to call.

    % Increment input argument
    function res = incfnc(A)
    % A = number
    res = A + 1;
    end
  3. MATLAB creates the following signatures. For BeginInvoke:

    [System.IAsyncResult RetVal, 
    double scalar refArg]	
        BeginInvoke	(
            SignatureExamples.delrefvoid this, 
            double scalar refArg, 
            System.AsyncCallback callback, 
            System.Object object)
    

    The EndInvoke signature:

    double scalar refArg	
        EndInvoke	(
            SignatureExamples.delrefvoid this, 
            double scalar refArg, 
            System.IAsyncResult result)
    
  4. Call incfnc.

    x = 6;
    myDel = SignatureExamples.delrefvoid(@incfnc);
    asyncRes = myDel.BeginInvoke(x,[],[]);
    while asyncRes.IsCompleted ~= true
        pause(0.05) % Use pause() to let MATLAB process event
    end
    myRef = 0;
    result = myDel.EndInvoke(myRef,asyncRes);
    disp(['Increment of ' num2str(x) ' = ' num2str(result)]);
    
    Increment of 6 = 7

This example shows how to use a delegate with an out parameter, argOut, and one return value.

  1. C# delegate:

    public delegate Single deloutsingle(Single argIn, out Single argOut);
    
  2. MATLAB maps the out argument as a return value for a total of two return values. MATLAB function to call.

    % Double input argument
    function [res1,res2] = times2fnc(A)
    res1 = A*2;
    res2 = res1;
    end
  3. MATLAB creates the following signatures. For BeginInvoke.

    [System.IAsyncResult RetVal, 
    single scalar argOut]	
        BeginInvoke	(
            SignatureExamples.deloutsingle this, 
            single scalar argIn, 
            System.AsyncCallback callback, 
            System.Object object)
    

    The EndInvoke signature is:

    [single scalar RetVal, 
    single scalar argOut]	
        EndInvoke	(
            SignatureExamples.deloutsingle this, 
            System.IAsyncResult result)
    
  4. Call times2fnc.

    myDel = SignatureExamples.deloutsingle(@times2fnc);
    asyncRes = myDel.BeginInvoke(6,[],[]);
    while asyncRes.IsCompleted ~= true
        pause(0.05) % Use pause() to let MATLAB process event
    end
    [a1,a2] = myDel.EndInvoke(asyncRes);
    a1
    
    a1 =
        12

Tips

Introduced in R2011a