Why does my .NET Framework application using MWArray hang after execution on Windows Server 2025?

Why does my .NET Framework application not terminate after execution when using MWArray and MATLAB Runtime on Windows Server 2025?
I have a .NET Framework v4.7.2 application that uses the MWArray API and MATLAB Runtime. On Windows Server 2025, the application reaches the end of execution and prints all expected output, but the process does not exit. The same application terminates normally on Windows 11 and Windows Server 2022.
Additionally, my application may be deployed as a long-running Windows Service that processes multiple MATLAB requests over its lifetime. How can I ensure that the application exits correctly, and when should MATLAB Runtime be terminated in this type of workflow?

 Accepted Answer

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.
For more information, please refer to: MWMCR - Read CTF bytes from stream 
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.

More Answers (0)

Products

Release

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!