如何通过 PID 向 CMD 进程发送关闭命令
How can i send close command to a CMD process by PID
这个进程是由selenium启动的
所以我无法控制它的启动方式
但是CMD进程有X按钮
我可以通过 PID 编程调用那个 X 按钮吗?
C# 4.6.2
windows8.1
我可以终止该进程,但它的效果与单击 X 按钮的效果不同
您需要找到 window and send message 到 window
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
如描述here
这应该可以解决问题:
// Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close();
这是我使用系统命令 taskkill 的部分代码:
//starts a command
public static int RunCommandAndWait(string command, string args)
{
int result = -1;
Process p = new Process();
p.StartInfo.Arguments = args;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = true;
if (p.Start())
{
p.WaitForExit();
result = p.ExitCode;
}
p.Dispose();
return result;
}
//kills a process using its pid and above method
public static void KillProcessTree(int processId)
{
RunCommandAndWait("taskkill", String.Format("/pid {0} /f /t", processId));
try
{
Process p = Process.GetProcessById(processId);
if (p != null)
KillProcessTree(processId); //this sometimes may be needed
}catch(Exception)
{
}
}
这个进程是由selenium启动的
所以我无法控制它的启动方式
但是CMD进程有X按钮
我可以通过 PID 编程调用那个 X 按钮吗?
C# 4.6.2 windows8.1
我可以终止该进程,但它的效果与单击 X 按钮的效果不同
您需要找到 window and send message 到 window
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
如描述here
这应该可以解决问题:
// Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close();
这是我使用系统命令 taskkill 的部分代码:
//starts a command
public static int RunCommandAndWait(string command, string args)
{
int result = -1;
Process p = new Process();
p.StartInfo.Arguments = args;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = true;
if (p.Start())
{
p.WaitForExit();
result = p.ExitCode;
}
p.Dispose();
return result;
}
//kills a process using its pid and above method
public static void KillProcessTree(int processId)
{
RunCommandAndWait("taskkill", String.Format("/pid {0} /f /t", processId));
try
{
Process p = Process.GetProcessById(processId);
if (p != null)
KillProcessTree(processId); //this sometimes may be needed
}catch(Exception)
{
}
}