Programmatically interrupt MATLAB on Windows

When using MATLAB through the GUI, I can easily interrupt a computation by pressing Ctrl-C.
Is there a way to do the same programmatically when using MATLAB through the MATLAB Engine C API?
On Unix systems there is a solution: send a SIGINT signal. This will not kill MATLAB. It'll only interrupt the computation (i.e. exactly what I want). I am looking for a solution that works on Windows.
Use case: this is for a Mathematica-MATLAB interface. I want to be able to forward interrupts from Mathematica to MATLAB. The functionality is working and implemented on Linux/OSX, but I'd prefer to have it for the majority Windows platform as well.
EDIT: I'm also interested in solution that work with the MEX interface. The MEX function would be multi-threaded where the first thread is running an evalc(), and the second is watching for interrupt conditions. How would the second thread interrupt the evalc()? I have less hope for this with MEX than with Engine though.
UPDATE: I still haven't found any way to do this with the Engine interface, but I did find something which might work with MEX. The libut library has a function called utSetInterruptPending(). When running sequentially, utSetInterruptPending(1) will interrupt MATLAB (as tested with calllib()). Does anyone know utSetInterruptPending() is safe to call in a MEX function from a different thread than the main one?

7 comentarios

Iain
Iain el 20 de Mayo de 2013
I don't know what communications can exist between mathematica and matlab, however, you could poll for some kind of interrupt, and break out after receiving it.
Szabolcs
Szabolcs el 20 de Mayo de 2013
@iain The question is not specific to how the communication works. Please forget about Mathematica for a while, and imagine that you're writing a custom GUI for MATLAB and you're calling MATLAB through the Engine interface. How would you interrupt a long computation? The users of your GUI would want to be able to do this. Is the question more clear now?
Iain
Iain el 20 de Mayo de 2013
I understood the question, but I don't know the answer. - Which is why I suggested an alternative method that could allow you to temporaily interrupt the execution and do something else before continuing.
Szabolcs
Szabolcs el 20 de Mayo de 2013
Editada: Szabolcs el 20 de Mayo de 2013
@Iain I don't understand what you are saying. What do you mean by " poll for some kind of interrupt, and break out after receiving it."? I don't have to poll, I know exactly when I receive the interrupt message (I receive it on a different thread). What do you mean by "break out", or "temporarily interrupt"? The precise problem is that I don't know how to stop MATLAB at all on Windows, either temporarily or permanently. The only thing I can do is make it quit completely.
Iain
Iain el 21 de Mayo de 2013
I mean if you, in your matlab code, check to see if something has been done, say, a file being written to a known location, and if so, have the matlab code either stop execution, or, say, execute the code in that file.
Szabolcs
Szabolcs el 21 de Mayo de 2013
@Iain I have no control over the code that's being run, so I can't do that.

Iniciar sesión para comentar.

Respuestas (3)

Walter Roberson
Walter Roberson el 20 de Mayo de 2013

0 votos

MS Windows XP SP2 and later support POSIX signals including (presumably) SIG_INT.

2 comentarios

Szabolcs
Szabolcs el 20 de Mayo de 2013
How can I send such a signal? And does MATLAB actually respond to it the same way as on Unix? I did find that Windows should support these signals, but I have not found a way to actually send them to other processes. The signal.h header does not have a kill() function like on Unix.
I did find this too, but that seems to be for command line programs only, and it doesn't seem to work at all when I try (I get a permission denied error).
Walter Roberson
Walter Roberson el 27 de Mzo. de 2020
Editada: Walter Roberson el 8 de Jun. de 2020
It looks like the various tools available to send a signal to an id, do so by sending a WM_CLOSE or WM_TERMINATE message. Unfortunately MATLAB would not be able to tell those apart from user request to terminate the program.
I find evidence that console applications can bind in handlers for control-break, but matlab is not generally a console application and I do not know if it does that binding (and I have not seen yet how a different process can provoke it)

Iniciar sesión para comentar.

Jan
Jan el 21 de Mayo de 2013

0 votos

I assume, you do not want to stop the engine temporarily, like Ctrl-C in the application, but kill the job. Then the windows command taskkill will help.

3 comentarios

Szabolcs
Szabolcs el 21 de Mayo de 2013
Actually what I need is interrupting MATLAB without killing it, i.e. the equivalent of Ctrl-C. (Quoting from the question: "This will not kill MATLAB. It'll only interrupt the computation (i.e. exactly what I want).") Killing it is easy, but I'm looking for a way to interrupt it without killing it.
Jan
Jan el 22 de Mayo de 2013
What do you expect as internal state of an interrupted engine? When Matlab is not run as application with a GUI, I cannot imagine, what interrupting without killing can be useful for. Do you want to resume later one?
I have an example where I'd like to do this. I am running DAQ toolbox audio data input using startBackground(session). This is run from a callback function followed by wait(session). MATLAB is Busy, unless I use CTRL-C to stop it. I'd like to map a button from the UI to stop it in the same manner.

Iniciar sesión para comentar.

Szabolcs
Szabolcs el 27 de Mzo. de 2020

0 votos

I originally asked this question a very long time ago, when MATLAB only provided a buggy and limited C API (the "MATLAB Engine") for controlling it from another application.
Since then, MATLAB introduced a much better and much more flexible C++ Engine API.
I have not yet used this, but according to the documentation, it does have facilities for asynchronous evaluations and fo interruption.
See FutureResult and its cancel() method.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 20 de Mayo de 2013

Editada:

el 8 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by