Main Content

EndInvoke

检索 .NET System.Delegate BeginInvoke 方法启动的异步调用结果

    说明

    示例

    result = EndInvoke(asyncResult) 检索 Microsoft® .NET Framework 应用程序的 BeginInvoke 方法启动的异步调用结果。有关异步调用同步方法的信息,请参考 Microsoft .NET 文档。

    注意

    对于使用 .NET Framework 4.0 和更高版本(包括 .NET 5 和 .NET Core 及更高版本)的应用程序,请使用基于任务的 API,如 System.Threading.Tasks。有关详细信息,请参阅 Microsoft 文章 .NET 中基于任务的异步模式 (TAP)

    示例

    [res0,...,resN] = EndInvoke(res0,...,resN,asyncResult)(对于带 out 和/或 ref 参数的方法)。

    示例

    全部折叠

    del2int 委托返回两个输入参量的结果。委托没有 outref 参数。

    将此 C# 委托代码构建到名为 SignatureExamples 的程序集中,并将其加载到 MATLAB® 中。有关信息,请参阅Build a .NET Application for MATLAB Examples

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

    创建此 MATLAB 函数以将两个整数相加。

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

    创建委托并显示 BeginInvoke 签名。

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

    查看 EndInvoke 签名。

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

    调用 addfcn

    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

    delrefvoid 委托使用 ref 参数 (refArg)。MATLAB 将 ref 参量映射为 RHS 和 LHS 参量。

    将此 C# 委托代码构建到名为 SignatureExamples 的程序集中,并将其加载到 MATLAB 中。

    public delegate void delrefvoid(ref Double refArg);
    

    创建此 MATLAB 函数来递增输入参量并返回结果。

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

    创建委托并显示 BeginInvoke 签名。

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

    查看 EndInvoke 签名。

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

    调用 incfcn

    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

    deloutsingle 委托使用 out 参数 (argOut) 和一个返回值。MATLAB 将 out 参量映射为附加返回值。

    将此 C# 委托代码构建到名为 SignatureExamples 的程序集中,并将其加载到 MATLAB 中。

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

    创建此 MATLAB 函数以使输入参量倍增。

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

    创建委托并显示 BeginInvoke 签名。

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

    查看 EndInvoke 签名。

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

    调用 times2fcn

    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

    输入参数

    全部折叠

    BeginInvoke 返回的对象,指定为 .NET System.IAsyncResult 对象。

    委托返回的异步调用的结果 0 到 N(如果有),指定为任一有效类型。对于带 out 和/或 ref 参数的方法。参量数量是以下项的总和:

    • 返回值(01)的数目。

    • outref 参量的数目。

    输出参量

    全部折叠

    异步调用的结果,以任一有效类型形式返回。

    委托返回的结果 0 到 N(如果有),以任一有效类型形式返回。对于带 out 和/或 ref 参数的方法。

    版本历史记录

    在 R2011a 中推出