Matlab onCleanup with Compiled applications (windows)
Asked Answered
A

1

6

I have an application made with matlab compiler.
I want to do some shutdown activities whenever it ends. As it seems to be impossible to catch signals in matlab (or I'm not able to), I checked to use onCleanup (Matlab: Is it possible to create signal handlers (.m scripts)). It is working within matlab (native), but not within the compiled application.
I tried to end the application with CTRL-C and with taskkill (which only work with /f). In both cases the onCleanup-method was NOT executed.
For testing purposes here

function sigtest(varargin)
remainder=onCleanup(@()save('exit.mat'));
b=1;
while true
    disp(datestr(now));
    a=rand(round(5*b));%to be saved
    pause(10);
    b=a(1);
end

my source code, which I compiled via mcc -m -v sigtest.m.
As onether try, I inserted the lines

myexiter=addlistener(System.AppDomain.CurrentDomain,'ProcessExit',...
    @(a,b)save('listexit.mat'));

after line 2, but also this .NET-Event is not working.

Aseity answered 3/1, 2013 at 13:36 Comment(0)
D
4

If you're registering shutdown activities within M-code, they're only going to work on a graceful shutdown of the process. The taskkill /f command will do a "forceful" shutdown, which I think will terminate the process immediately. The Matlab interpreter won't get a chance to run whatever cleanup code is still pending. I think Ctrl-C on a console application (which the compiled sigtest.m will be running as) will have the same effect. Same applies to the .NET-Event: if you forcefully kill the process, that callback never gets a chance to run.

If you want on-exit code, or any other cleanup stuff, to run, you need to find a way for the program to find out when it should exit and initiate a more graceful shutdown itself. For example, in your sigtest example, you could check stdin at the end of every pass through the loop, see if the user has typed 'quit', and if so call exit(). Then your onCleanup stuff should run.

In a GUI compiled Matlab application, this is more straightforward; you have GUI controls to exit the application. I don't know what the canonical way is to make a console compiled Matlab application responsive to user exit requests, or if there even is a good one. You might want to make this a GUI app if you think the user might want to request a graceful abort of its operation.

Dory answered 4/1, 2013 at 7:0 Comment(3)
Thanks for your explanations - which have been clear to me before. The user should NOT be able to interact. I'm tending to implement that process as a windows service and therefore I want to trap shutdown events. Thus a GUI is not a solution. Maybe one can find the appropriate methods within those GUI-files? Do you have a hint?Aseity
Ah. Yeah, no GUI then. You'll probably want to code your Windows Service startup and shutdown hooks to call mclInitialize()/libXxxxInitialize() and libXxxTerminate()/mclTerminate() respectively. (I'd look at using mcc -l to compile your M-code to a library instead of a standalone app, and then use Vis Studio to build a Windows Service project that links in your DLL.) The "stop service" WS hook should tell the Matlab code to quit. Don't know the "right" way to do this - maybe having it set a global Matlab variable that your M-code checks in its top level loop and exits when set. Make sense?Dory
FYI, here's another question talking about programmatically doing Ctrl-C behavior, which is effectively what you're looking for here, I think. #10033578 Unfortunately, all the answers use GUI stuff so I don't think they'll work for you here.Dory

© 2022 - 2024 — McMap. All rights reserved.