Stopping execution of process in application

  • Thread starter Thread starter VMI
  • Start date Start date
V

VMI

My Windows form has two buttons: "Start" and "Stop". How can I stop the form
from doing whatever it is doing without closing the whole application? The
"Stop" button would have to stop a process that's being run in another
class, not from within the form.
Thanks.
 
Hi,

It depends on the process being run.
I suggest you to run the process within a worker thread and use some sort of
synchronization mechanism to inform the process that it should stop
(ManualResetEvent perhaps).
Or, if you are careful enough you might use Thread.Abort (not recommended).
 
VMI said:
My Windows form has two buttons: "Start" and "Stop". How can I stop
the form from doing whatever it is doing without closing the whole
application? The "Stop" button would have to stop a process that's
being run in another class, not from within the form.

Do you mean an actual other process, or just another thread?

Have a look at
http://www.pobox.com/~skeet/csharp/multithreading.html#worker.threads
for code to shut down a worker thread gracefully.
 
How do I know which process I need to kill? How would I know what process ID
was generated when I started the process?
Thanks.
 
VMI said:
How do I know which process I need to kill? How would I know what
process ID was generated when I started the process?

You'd keep hold of the Process reference from when you started it.
 
I'm still not sure I understand. In the MSDN, the example shown is for
starting a process by running an application outside of the project. How can
I get the Process reference when I call the method that'll be executed?

Thanks again.
 
VMI said:
I'm still not sure I understand. In the MSDN, the example shown is for
starting a process by running an application outside of the project. How can
I get the Process reference when I call the method that'll be executed?

Which example are you talking about?

The static Process.Start methods return a Process reference - that's
the thing to keep hold of so you can call Process.Kill/CloseMainWindow
on it later.
 
From what I've seen in the web and MSDN, Process.Start() is used to run an
external application (ie. Process.Start("notepad.exe", "myfile.txt") ). In
my Windows app, I'm trying to stop a method that's being executed in another
class:

private void btn_start_Click(object sender, System.EventArgs e)
{
ZMMatch.Output myOutput = new ZMMatch.Output();
myOutput.writeOutputFile();
}
private void btn_stop_Click(object sender, System.EventArgs e)
{
/* Here I want to stop myOutput.writeOutputFile() from running */
}

Thanks.
 
VMI said:
From what I've seen in the web and MSDN, Process.Start() is used to run an
external application (ie. Process.Start("notepad.exe", "myfile.txt") ). In
my Windows app, I'm trying to stop a method that's being executed in another
class:

private void btn_start_Click(object sender, System.EventArgs e)
{
ZMMatch.Output myOutput = new ZMMatch.Output();
myOutput.writeOutputFile();
}
private void btn_stop_Click(object sender, System.EventArgs e)
{
/* Here I want to stop myOutput.writeOutputFile() from running */
}

If you're only using a single thread as you said before, then
myOutput.writeOutputFile *isn't* running, unless you call
Application.DoEvents within writeOutputFile (which is a bad idea, IMO -
I'm not a fan of DoEvents).
 
Yes, I'm using Application.DoEvents() in writeOutputFile() and everything's
in a single thread. With that, would it be possible to stop execution of the
method?
I also hate using Application.DoEvents() but I haven't had the time to fix
it.

Thanks again.
 
VMI said:
Yes, I'm using Application.DoEvents() in writeOutputFile() and everything's
in a single thread. With that, would it be possible to stop execution of the
method?

Just set a flag in the "stop" method, and test the flag in
writeOutputFile periodically - if it's been set, abort the operation.
I also hate using Application.DoEvents() but I haven't had the time to fix
it.

Creating a new thread and using that doesn't take *that* long...
 
Back
Top