This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Calling .NET Methods Asynchronously

How MATLAB Handles Asynchronous Method Calls in .NET

It is possible to call a synchronous method asynchronously in MATLAB®. With some modifications, you can use the Microsoft® BeginInvoke and EndInvoke methods. For more information, refer to the MSDN® article Calling Synchronous Methods Asynchronously.

You can use delegates to call a synchronous method asynchronously by using the BeginInvoke and EndInvoke methods. If the thread that initiates the asynchronous call does not need to be the thread that processes the results, you can execute a callback method when the call completes. For information about using a callback method, see Calling a Method Asynchronously Using a Callback When an Asynchronous Call Finishes.

Note

MATLAB is a single-threaded application. Therefore, handling asynchronous calls in the MATLAB environment might result in deadlocks.

Calling a Method Asynchronously Using a Callback When an Asynchronous Call Finishes

You can execute a callback method when an asynchronous call completes. A callback method executes on a different thread than the thread that processes the results of the asynchronous call.

The following is an overview of the procedure. If you do not use a callback function, follow the procedure in Calling a Method Asynchronously Without a Callback.

  • Select or create a MATLAB function to execute asynchronously.

  • Select or create a C# delegate and associate it with the MATLAB function.

  • Create a MATLAB callback function with a System.AsyncCallback Delegate signature. The signature, shown at the MSDN website, is:

    public delegate void AsyncCallback(IAsyncResult ar)
    
  1. Using MATLAB code, initiate the asynchronous call using the BeginInvoke method, specifying the callback delegate and, if required, object parameters.

  2. Continue executing commands in MATLAB.

  3. When the asynchronous function completes, MATLAB calls the callback function, which executes the EndInvoke method to retrieve the results.

Callback Example

In this example, create the following MATLAB function to execute asynchronously:

function X = DivideFunction(A, B)
if B ~= 0
    X = A / B;
else
    errid = 'MyID:DivideFunction:DivisionByZero';
    error(errid, 'Division by 0 not allowed.')
end
end

Create the following MATLAB function, which executes as the callback when the asynchronous method invocation completes. This function displays the result value of the EndInvoke method.

function myCallback(asyncRes)
result = asyncRes.AsyncDelegate.EndInvoke(asyncRes);
disp(result)
end

Use the del2Integer delegate, defined in the NetDocDelegate assembly:

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

Run the example:

% Create the delegate
divDel = NetDocDelegate.del2Integer(@DivideFunction);
A = 10;
B = 5;
% Initiate the asynchronous call.
asyncRes = divDel.BeginInvoke(A,B,@myCallback,[]);

MATLAB displays the result: 2

Calling a Method Asynchronously Without a Callback

The following is an overview of the procedure. If you want to use a callback function, follow the procedure in Calling a Method Asynchronously Using a Callback When an Asynchronous Call Finishes.

  • Select or create a MATLAB function to execute asynchronously.

  • Select or create a C# delegate and associate it with the MATLAB function.

  1. In MATLAB, initiate the asynchronous call using the BeginInvoke method.

  2. Continue executing commands in MATLAB.

  3. Poll for asynchronous call completion using the MATLAB pause function.

  4. When the asynchronous function completes, call the EndInvoke method to retrieve the results.

Example Without Callback

In this example, create the following MATLAB function, myFunction:

% MATLAB function to execute asynchrounously
function res = myFunction(strValue)
res = strValue;
end

Use the delString delegate, defined in the NetDocDelegate assembly:

public delegate string delString(string message);

In MATLAB, create the delegate, myDelegate, define the input values, and start the asynchronous call:

myDelegate = NetDocDelegate.delString(@myFunction);
A = 'Hello';
asyncRes = myDelegate.BeginInvoke(A,[],[]);

The BeginInvoke method returns the object, asyncRes, which you use to monitor the progress of the asynchronous call. Poll for results, using the MATLAB pause function to let MATLAB process the events:

while asyncRes.IsCompleted ~= true
    pause(0.01)
end

Retrieve and display the results of the asynchronous call:

result = myDelegate.EndInvoke(asyncRes);
disp(result)
Hello

Using EndInvoke With out and ref Type Arguments

The MATLAB delegate signature for EndInvoke follows special mapping rules if your delegate has out or ref type arguments. For information about the mapping, see .NET Delegates With out and ref Type Arguments. For examples, see the EndInvoke reference page.

Using Polling to Detect When Asynchronous Call Finishes

For MATLAB to process the event that executes the delegate callback on the main thread, call the MATLAB pause (or a similar) function.

See Also

|

External Websites