tags:

views:

3322

answers:

3

I've written a C# application which uses System.Diagnostics.Process class to create a process, using

 Process P1 = new Process();
 P1.FileName = "myexe.exe";

and other proper settings.

I've linked it to an exe file which runs for about 10 minutes. (I'm writing program to measurer runtime of programs). Now in between i want to abort the running process. So I wrote in the cancel button's event,

 Process.Close();

But in the task manager i still see myexe.exe running, it doesn't get aborted. What to do?

+9  A: 

Process.Close() isn't meant to abort the process - it's just meant to release your "local" view on the process, and associated resources.

I think you mean Process.Kill() or Process.CloseMainWindow(). Personally I'd try to find a more graceful way of shutting it down though.

Jon Skeet
and what would that be? to flush all the streams and then send internal abort method if the underlying exe supports it?
Anirudh Goel
@Anirudh - I think he means "Why do you need to force close the app?". Aborting a process in the middle of execution is usually not favorable because it leads to data corruption.
StingyJack
I would usually try to indicate to the app "please shut down now" rather than just killing it. If you control the app, you could make it watch for its parent process going away.
Jon Skeet
+1  A: 

Use Process.Kill instead.

Dave Van den Eynde
+1  A: 

I think Process.Kill() is what you're looking for.

Krougan