Main Content

Call .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 information about calling synchronous methods asynchronously, refer to the Microsoft website.

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.

Note

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

Note

For applications using .NET Framework 4.0 and higher, including .NET 6 and .NET Core and later versions, use task-based APIs such as System.Threading.Tasks. For more information, see the Microsoft article Task-based asynchronous pattern (TAP) in .NET.

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

|