Reputation: 657
I have this code to run the exe:
String cPath = "C:\\GCOS\\HHT\\EXE\\" + frmSchemas.schema;
string cParams = HHTNUMBER+" "+ Login.user + "/" + Login.pass + "//" +Login.db + "//" + frmSchemas.schema ;
string filename = Path.Combine(cPath,"HHTCtrlp.exe");
Process.Start(filename, cParams);
Now how do i end the program above?
Upvotes: 2
Views: 21875
Reputation: 7888
System.Diagnostics.Process.Start("c:\\windows\\system32\\notepad.exe");
System.Diagnostics.Process q;
q = System.Diagnostics.Process.GetProcessesByName("notepad")[0];
q.Kill();
Upvotes: 0
Reputation: 15906
Assign your process to a variable and call the Kill method. i.e.
String cPath = "C:\\GCOS\\HHT\\EXE\\" + frmSchemas.schema;
string cParams = HHTNUMBER+" "+ Login.user + "/" + Login.pass + "//" +Login.db + "//" + frmSchemas.schema ;
string filename = Path.Combine(cPath,"HHTCtrlp.exe");
var p = Process.Start(filename, cParams);
// ... Later in Code ...
p.Kill();
Upvotes: 0
Reputation: 490
Process[] processes = Process.GetProcessesByName("HHTCtrlp");
foreach (var process in processes)
{
process.Kill();
}
Upvotes: 10
Reputation: 62472
Process.Start will return a Process instance. You can call Kill on the instance to terminate the process.
Upvotes: 1
Reputation: 15557
You need the process ID to kill it.
foreach (Process proc in Process.GetProcesses())
{
if (proc.Id == _processID)
{
proc.Kill();
}
}
Upvotes: 0
Reputation: 137108
Keep a handle to the process - Process.Start
returns a Process
object.
You can then use (in extremis):
process.Kill();
to stop it.
Using:
process.CloseMainWindow();
is probably a better way (assuming the process has a UI)
Upvotes: 1
Reputation: 132974
Here's a sample from http://csharp-slackers.blogspot.com/2008/09/terminate-process.html
using System;
using System.Threading;
using System.Diagnostics;
public class TerminateProcessExample {
public static void Main () {
// Create a new Process and run notepad.exe.
using (Process process = Process.Start("notepad.exe")) {
// Wait for 5 seconds and terminate the notepad process.
Console.WriteLine("Waiting 5 seconds before terminating" +
" notepad.exe.");
Thread.Sleep(5000);
// Terminate notepad process.
Console.WriteLine("Terminating Notepad with CloseMainWindow.");
// Try to send a close message to the main window.
if (!process.CloseMainWindow()) {
// Close message did not get sent - Kill Notepad.
Console.WriteLine("CloseMainWindow returned false - " +
" terminating Notepad with Kill.");
process.Kill();
} else {
// Close message sent successfully; wait for 2 seconds
// for termination confirmation before resorting to Kill.
if (!process.WaitForExit(2000)) {
Console.WriteLine("CloseMainWindow failed to" +
" terminate - terminating Notepad with Kill.");
process.Kill();
}
}
}
// Wait to continue.
Console.WriteLine("Main method complete. Press Enter.");
Console.ReadLine();
}
}
As you can see, there are more graceful ways to try to terminate a process than just using Process.Kill();
Upvotes: 2
Reputation: 9249
Process proc = Process.Start(filename, cParams);
// ....
proc.CloseMainWindow();
proc.Close();
// ...or the rude way ;) ...
proc.Kill();
Upvotes: 0
Reputation: 29668
You can keep hold of the process you start:
var example_process = Process.Start("notepad.exe");
And later:
example_process.Kill();
Upvotes: 0