If your application uses the MWArray API, explicitly terminate MATLAB Runtime before the application exits.
Add the following namespace:
using MathWorks.MATLAB.NET.Utility;
For applications that are exiting after completing their work, call:
MWMCR.TerminateApplication();
immediately before the application exits.
"MWMCR.TerminateApplication()" can allow affected .NET Framework applications using MWArray and MATLAB Runtime to terminate correctly on Windows Server 2025 instead of remaining active after execution completes.
When using "MWMCR.TerminateApplication()":
- Call it once per process.
- Call it only when the application is completely finished using MATLAB Runtime.
- After calling it, do not make any additional MATLAB Runtime or MATLAB Compiler SDK calls from the same process.
For long-running Windows services, use:
MWMCR.TerminateApplicationEx();
during service shutdown.
"MWMCR.TerminateApplicationEx()" is the recommended API when explicitly tearing down MATLAB Runtime as part of a controlled application or service shutdown. When implementing a long-running service, follow these guidelines:
- Create and initialize MATLAB Runtime when the service starts.
- Reuse the same MATLAB Runtime instance while processing requests.
- Avoid repeatedly initializing and terminating MATLAB Runtime for each request.
- Shut down MATLAB Runtime only when the service is stopping.
A short example pseudocode:
using MathWorks.MATLAB.NET.Utility;
// --- Service Startup ---
matlabInstance = new MATLABClass() // creates MCR instance, loads CTF
// --- Request Processing Loop ---
while service is running:
request = queue.dequeue() // block until a request arrives
matlabInstance.SomeFunction(request.inputs)
// --- Service Shutdown ---
MWMCR.TerminateApplicationEx()
Note: The "MathWorks.MATLAB.Runtime.MATLABRuntime" APIs documented for the MATLAB Data API are separate from the "MWMCR" APIs used by applications based on the MWArray API. Applications using MWArray should use the "MathWorks.MATLAB.NET.Utility.MWMCR" APIs when implementing MATLAB Runtime shutdown behavior.