Main Content

EndInvoke

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

    Description

    example

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

    example

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

    Examples

    collapse all

    The del2int delegate returns the result of two input arguments. The delegate does not have out or ref parameters.

    Build this C# delegate code into an assembly named SignatureExamples and load it into MATLAB®. For information, see Build a .NET Application for MATLAB Examples.

    public delegate Int32 del2int(Int32 arg1, Int32 arg2);
    

    Create this MATLAB function to add two integers.

    % Add input arguments
    function res = addfnc(A, B)
    % A and B are numbers
    res = A + B;
    end
    

    Create the delegate and display the BeginInvoke signature.

    myDel = SignatureExamples.del2int(@addfnc);
    methodsview(myDel)
    System.IAsyncResult RetVal	
        BeginInvoke	(
            SignatureExamples.del2int this, 
            int32 scalar arg1, 
            int32 scalar arg2, 
            System.AsyncCallback callback, 
            System.Object object)
    

    Review the EndInvoke signature.

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

    Call 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

    The delrefvoid delegate uses a ref parameter (refArg). MATLAB maps the ref argument as both RHS and LHS arguments.

    Build this C# delegate code into an assembly named SignatureExamples and load it into MATLAB.

    public delegate void delrefvoid(ref Double refArg);
    

    Create this MATLAB function to increment the input argument and return the result.

    % Increment input argument
    function res = incfnc(A)
    % A = number
    res = A + 1;
    end

    Create the delegate and display the BeginInvoke signature.

    myDel = SignatureExamples.delrefvoid(@incfnc);
    methodsview(myDel)
    [System.IAsyncResult RetVal, 
    double scalar refArg]	
        BeginInvoke	(
            SignatureExamples.delrefvoid this, 
            double scalar refArg, 
            System.AsyncCallback callback, 
            System.Object object)
    

    Review the EndInvoke signature.

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

    Call incfnc.

    x = 6;
    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

    The deloutsingle delegate uses an out parameter (argOut) and one return value. MATLAB maps the out argument as an additional return value.

    Build this C# delegate code into an assembly named SignatureExamples and load it into MATLAB.

    public delegate Single deloutsingle(Single argIn, out Single argOut);
    

    Create this MATLAB function to double the input argument.

    % Double input argument
    function [res1,res2] = times2fnc(A)
    res1 = A*2;
    res2 = res1;
    end

    Create the delegate and display the BeginInvoke signature.

    myDel = SignatureExamples.deloutsingle(@times2fnc);
    methodsview(myDel)
    [System.IAsyncResult RetVal, 
    single scalar argOut]	
        BeginInvoke	(
            SignatureExamples.deloutsingle this, 
            single scalar argIn, 
            System.AsyncCallback callback, 
            System.Object object)
    

    Review the EndInvoke signature.

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

    Call 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

    Input Arguments

    collapse all

    Object returned by BeginInvoke, specified as a .NET System.IAsyncResult object.

    Results 0 through N (if any) of the asynchronous call returned by the delegate, specified as any valid type. For methods with out and/or ref parameters. The number of arguments is the sum of:

    • Number of return values (0 or 1).

    • Number of out and ref arguments.

    Output Arguments

    collapse all

    Result of the asynchronous call, returned as any valid type.

    Results 0 through N (if any) returned by the delegate, returned as any valid type. For methods with out and/or ref parameters.

    Introduced in R2011a