Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to stop a running program

Subject: how to stop a running program

From: Marcio

Date: 6 Oct, 2008 18:59:02

Message: 1 of 16

Please, dear friends,

Do you know how to stop a running program? I have a DLL file that was generated by mex from a C file, this program runs and the only way of getting it to stop is by quiting MATLAB (ctrl+alt+del), but I think there must be something clever than that.

Thanks for commenting.

Subject: how to stop a running program

From: someone

Date: 6 Oct, 2008 19:07:02

Message: 2 of 16

"Marcio " <marciobarbalho@yahoo.com> wrote in message <gcdn5m$5qt$1@fred.mathworks.com>...
> Please, dear friends,
>
> Do you know how to stop a running program? I have a DLL file that was generated by mex from a C file, this program runs and the only way of getting it to stop is by quiting MATLAB (ctrl+alt+del), but I think there must be something clever than that.
>
> Thanks for commenting.

Try Ctrl+C

Subject: how to stop a running program

From: Walter Roberson

Date: 6 Oct, 2008 19:32:44

Message: 3 of 16

Marcio wrote:
 
> Do you know how to stop a running program? I have a DLL file that was generated by
> mex from a C file, this program runs and the only way of getting it to stop is
> by quiting MATLAB (ctrl+alt+del), but I think there must be something clever than that.

If control-C doesn't work, then No, there really isn't anything else, other than
re-writing the code so that it actively checks whether it should terminate itself,
or so that it knows how to handle other kinds of signals and reacts to them by
terminating itself.

When you run a function from a DLL, it does not run as an independent program:
it runs as part of matlab itself.

Subject: how to stop a running program

From: Marcio

Date: 7 Oct, 2008 01:28:02

Message: 4 of 16

Walter Roberson <roberson@hushmail.com> wrote in message

> If control-C doesn't work, then No, there really isn't anything else, other than > re-writing the code so that it actively checks whether it should terminate itself, > or so that it knows how to handle other kinds of signals and reacts to them by > terminating itself.
>
> When you run a function from a DLL, it does not run as an independent program: > it runs as part of matlab itself.

I have no idea on how to put something in the m-file that can make the code stop itself, all I can do is pause it once. Whether the function "belongs to MATLAB", ctrl+C stops it, otherwise... it's been a problem!

Subject: how to stop a running program

From: ImageAnalyst

Date: 7 Oct, 2008 02:28:21

Message: 5 of 16

On Oct 6, 3:07=A0pm, "someone " <some...@somewhere.net> wrote:
> "Marcio " <marciobarba...@yahoo.com> wrote in message <gcdn5m$5q...@fred.=
mathworks.com>...
> > Please, dear friends,
>
> > Do you know how to stop a running program? I have a DLL file that was g=
enerated by mex from a C file, this program runs and the only way of gettin=
g it to stop is by quiting MATLAB (ctrl+alt+del), but I think there must be=
 something clever than that.
>
> > Thanks for commenting.
>
> Try Ctrl+C

----------------------
Ctrl-C is supposed to work but in my experience, it actually very,
very rarely works. In Windows XP, as a shortcut, try control-shift-
Esc rather than ctrl-alt-del to go DIRECTLY to the task list rather
than have that annoying intermediate screen.
Good luck,
ImageAnalyst

Subject: how to stop a running program

From: Marcio

Date: 7 Oct, 2008 09:18:02

Message: 6 of 16

ImageAnalyst <imageanalyst@mailinator.com> wrote in message

> > Try Ctrl+C
>
> ----------------------
> Ctrl-C is supposed to work but in my experience, it actually very,
> very rarely works. In Windows XP, as a shortcut, try control-shift-
> Esc rather than ctrl-alt-del to go DIRECTLY to the task list rather
> than have that annoying intermediate screen.
> Good luck,
> ImageAnalyst

Yeah, it's supposed to, but it does not stop the running program, in this particular case, ctrl+c do the opposite effect, I am putting some pauses in the program, ctrl+c makes the program go further without any stop. I can't see any solution but quit MATLAB by forcing it!

Subject: how to stop a running program

From: David

Date: 7 Oct, 2008 12:10:05

Message: 7 of 16

"Marcio " <marciobarbalho@yahoo.com> wrote in message <gcf9ga$qs8$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message
>
> > > Try Ctrl+C
> >
> > ----------------------
> > Ctrl-C is supposed to work but in my experience, it actually very,
> > very rarely works. In Windows XP, as a shortcut, try control-shift-
> > Esc rather than ctrl-alt-del to go DIRECTLY to the task list rather
> > than have that annoying intermediate screen.
> > Good luck,
> > ImageAnalyst
>
> Yeah, it's supposed to, but it does not stop the running program, in this particular case, ctrl+c do the opposite effect, I am putting some pauses in the program, ctrl+c makes the program go further without any stop. I can't see any solution but quit MATLAB by forcing it!

i have found that the on-off switch usually stops whatever is running.

Subject: how to stop a running program

From: Marcio

Date: 7 Oct, 2008 13:21:01

Message: 8 of 16

"David " <dave@bigcompany.com> wrote in message <gcfjit$bs6$1@fred.mathworks.com>...

> i have found that the on-off switch usually stops whatever is running.

I'm sorry, I didn't get it, what is exactly the "on-off switch"?

Thanks!

Subject: how to stop a running program

From: Steven Lord

Date: 7 Oct, 2008 15:10:32

Message: 9 of 16


"ImageAnalyst" <imageanalyst@mailinator.com> wrote in message
news:92d2a0e4-a34a-4126-831c-e1b46e074c9d@t65g2000hsf.googlegroups.com...
On Oct 6, 3:07 pm, "someone " <some...@somewhere.net> wrote:
> "Marcio " <marciobarba...@yahoo.com> wrote in message
> <gcdn5m$5q...@fred.mathworks.com>...
> > Please, dear friends,
> >
> > Do you know how to stop a running program? I have a DLL file that was
> > generated by mex from a C file, this program runs and the only way of
> > getting it to stop is by quiting MATLAB (ctrl+alt+del), but I think
> > there must be something clever than that.
> >
> > Thanks for commenting.
> >
> > Try Ctrl+C
>
> ----------------------
> Ctrl-C is supposed to work but in my experience, it actually very,
> very rarely works. In Windows XP, as a shortcut, try control-shift-
> Esc rather than ctrl-alt-del to go DIRECTLY to the task list rather
> than have that annoying intermediate screen.
> Good luck,
> ImageAnalyst

Ctrl-C sends MATLAB a signal that says "Stop what you're doing!" However,
if the actual routine that's computing is something else (like BLAS or
LAPACK for linear algebra operations, FFTW for the FFT, etc.) that's called
by MATLAB, MATLAB can't stop the computations until that other routine
returns control back to MATLAB and MATLAB has a chance to process the
signal. In order to make sure MATLAB has a chance to process the signal,
you may need to use DRAWNOW, PAUSE, FIGURE, or something else that gives
MATLAB a chance to process the pending event queue.

You may ask "Why can't MATLAB interrupt those other routines?" Well, if we
were to interrupt those routines, they might not be able to finish whatever
cleanup they need to perform at the end of their operation. That could lead
to memory leaks or memory corruption, and that would be a Bad Thing.

--
Steve Lord
slord@mathworks.com

Subject: how to stop a running program

From: David

Date: 7 Oct, 2008 16:52:01

Message: 10 of 16

"Steven Lord" <slord@mathworks.com> wrote in message <gcfu58$t8b$1@fred.mathworks.com>...
> You may ask "Why can't MATLAB interrupt those other routines?" Well, if we
> were to interrupt those routines, they might not be able to finish whatever
> cleanup they need to perform at the end of their operation. That could lead
> to memory leaks or memory corruption, and that would be a Bad Thing.


thats another advantage of the on-off switch, use that and you never get a memory leak!

Subject: how to stop a running program

From: David

Date: 7 Oct, 2008 17:19:02

Message: 11 of 16

"Marcio " <marciobarbalho@yahoo.com> wrote in message <gcfnnt$8pt$1@fred.mathworks.com>...
> "David " <dave@bigcompany.com> wrote in message <gcfjit$bs6$1@fred.mathworks.com>...
>
> > i have found that the on-off switch usually stops whatever is running.
>
> I'm sorry, I didn't get it, what is exactly the "on-off switch"?
>
> Thanks!

its usually a switch on the front of your computer. They used to be big switches on the side or back, but now they are usually small buttons on the front.

Subject: how to stop a running program

From: ImageAnalyst

Date: 8 Oct, 2008 01:35:04

Message: 12 of 16

On Oct 7, 11:10=A0am, "Steven Lord" <sl...@mathworks.com> wrote:
> "ImageAnalyst" <imageanal...@mailinator.com> wrote in message
>
> news:92d2a0e4-a34a-4126-831c-e1b46e074c9d@t65g2000hsf.googlegroups.com...
> On Oct 6, 3:07 pm, "someone " <some...@somewhere.net> wrote:
>
>
>
>
>
> > "Marcio " <marciobarba...@yahoo.com> wrote in message
> > <gcdn5m$5q...@fred.mathworks.com>...
> > > Please, dear friends,
>
> > > Do you know how to stop a running program? I have a DLL file that was
> > > generated by mex from a C file, this program runs and the only way of
> > > getting it to stop is by quiting MATLAB (ctrl+alt+del), but I think
> > > there must be something clever than that.
>
> > > Thanks for commenting.
>
> > > Try Ctrl+C
>
> > ----------------------
> > Ctrl-C is supposed to work but in my experience, it actually very,
> > very rarely works. =A0In Windows XP, as a shortcut, try control-shift-
> > Esc rather than ctrl-alt-del to go DIRECTLY to the task list rather
> > than have that annoying intermediate screen.
> > Good luck,
> > ImageAnalyst
>
> Ctrl-C sends MATLAB a signal that says "Stop what you're doing!" =A0Howev=
er,
> if the actual routine that's computing is something else (like BLAS or
> LAPACK for linear algebra operations, FFTW for the FFT, etc.) that's call=
ed
> by MATLAB, MATLAB can't stop the computations until that other routine
> returns control back to MATLAB and MATLAB has a chance to process the
> signal. =A0In order to make sure MATLAB has a chance to process the signa=
l,
> you may need to use DRAWNOW, PAUSE, FIGURE, or something else that gives
> MATLAB a chance to process the pending event queue.
>
> You may ask "Why can't MATLAB interrupt those other routines?" =A0Well, i=
f we
> were to interrupt those routines, they might not be able to finish whatev=
er
> cleanup they need to perform at the end of their operation. =A0That could=
 lead
> to memory leaks or memory corruption, and that would be a Bad Thing.
>
> --
> Steve Lord
> sl...@mathworks.com- Hide quoted text -
>
> - Show quoted text -

----------------------------------
So then do you recommend rebooting after we hit kill the MATLAB
process in order to thoroughly clean out litter left behind from
forcing termination?
Otherwise, the memory leaks and corruption that you mention will still
be there, as you say. How else to really clean it all up other than
rebooting?
Regards,
ImageAnalyst

Subject: how to stop a running program

From: Walter Roberson

Date: 8 Oct, 2008 16:44:57

Message: 13 of 16

ImageAnalyst wrote:

> So then do you recommend rebooting after we hit kill the MATLAB
> process in order to thoroughly clean out litter left behind from
> forcing termination?
> Otherwise, the memory leaks and corruption that you mention will still
> be there, as you say. How else to really clean it all up other than
> rebooting?

MS Windows will clean up when a process is terminated.
(I am not certain, though, that it will clean up anything that got
registered as a device driver. It would -probably- clean up loaded DLL's,
but I wouldn't stake my reputation on that without a lot of checking.)

When a registered signal handler is given control in any program compliant
with the relevant sections of the C or C++ standards, -everything-
would still be in place, and returning from the signal handler would
resume execution. If the signal handler executes a longjmp to return
to a previous point in the code instead of resuming where execution
had left off, then the call stacks are unwound, releasing any local
variables, but anything that was malloc'd or new'd in the routines
will be left untouched, and C does not provide any "garbage collection"
mechanisms to clean up those areas that there would then be no reference to.
There would, in this situation, also be no automatic closing of devices
or files or releasing of mmap'd or shared memory regions.

Subject: how to stop a running program

From: Walter Roberson

Date: 8 Oct, 2008 17:28:52

Message: 14 of 16

Marcio wrote:
> I have no idea on how to put something in the m-file that can make the code stop itself,

There are a number of different ways of doing it, and it is easy to get the details
wrong in the more efficient ways.

I suggest you experiment in a stand-alone program first. You can start with one
of the simpler mechanisms: each iteration of the loop, -attempt- to fopen() a
file with a particular name, such as 'STOPRUNNING.NOW'. If the fopen() fails
then keep executing the loop, but if the fopen succeeds, then leave the loop
and clean up. (Remember to fclose the file afterwards, but only if it actually
got successfully opened.)

Subject: how to stop a running program

From: Joe

Date: 15 Dec, 2010 21:56:05

Message: 15 of 16

This isn't an elegant solution, but it works and it sure beats powering off your computer. Unfortunately, if you haven't used AutoHotKey before, this might take a little longer than you want (~15-30 min).

You can get the effect of an in-line Ctl+C command by creating a relatively simple executable file to do that keystroke in the command window and then calling it in your code.

The key to this is using a program that can automate keystrokes (in this case, the Ctl+C keystroke). I did this using AutoHotKey but there may be other ways.

Method:
(1) Download and install AutoHotkey

(2) Open a text file and copy these lines into it:
sleep 20 ;this is a pause in milliseconds, adjust for your comp.
WinActivate, MATLAB ahk_class SunAwtFrame,, ;see below
sleep 20 ;this is a pause in milliseconds, adjust for your comp.
send ^c
sleep 20 ;this is a pause in milliseconds, adjust for your comp.
send ^c
sleep 20 ;this is a pause in milliseconds, adjust for your comp.
send ^c

(3) Use the AutoHotKey tool (Program files>AutoHotKey>Convert .ahk to .exe) to create a .exe out of that text notepad document and save it somewhere (henceforth %FileLocation%)

(4) In your matlab code, wherever you want to stop the code, enter the following commands:
  StopMatlabExeFilename = '%FileLocation%';
  open(StopMatlabExeFilename)
  pause(3) %Ensure file has time to execute

You'll see above that there are a few places you might have to edit. This "sleep" functions can be tweaked as noted. More importantly, the WinActivate function in the AutoHotKey script needs to correctly point to your matlab command window. To find the correct window and class information for your installation of Matlab, open the AutoIt3 Window Spy tool (Program files>AutoHotKey>AutoIt3 Window Spy), click on the Matlab command window, and take note of the info under "Window Title & Class". Edit the AutoHotKey script such that the text before "ahk_class" includes a word from the Window Title and the text after "ahk_class" is the same as the text in AutoIt3 Window Spy after the word "ahk_class".

Finally, it's important to emphasize that the pause line in the matlab cod is necessary. This is required to make sure the .exe file has time to execute.

Ok, that is all, hope this is helpful for someone.

Subject: how to stop a running program

From: Pooh

Date: 25 Jul, 2011 14:25:09

Message: 16 of 16

"Marcio " <marciobarbalho@yahoo.com> wrote in message <gcdn5m$5qt$1@fred.mathworks.com>...
> Please, dear friends,
>
> Do you know how to stop a running program? I have a DLL file that was generated by mex from a C file, this program runs and the only way of getting it to stop is by quiting MATLAB (ctrl+alt+del), but I think there must be something clever than that.
>
> Thanks for commenting.

Ctrl + break worked!!
Ctrl +C did not terminate the program

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us