【发布时间】:2018-12-20 12:00:29
【问题描述】:
我已经在我的 C# 应用程序中的按钮单击事件上启动了一个进程,如下所示,
System.Diagnostics.Process process = new System.Diagnostics.Process();
private void btnOpenPort_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "http-server";
// startInfo.Arguments = "/C http-server -p 8765";
this.process.StartInfo = startInfo;
this.process.Start();
}
现在,我想在应用程序窗口关闭时停止这个命令行进程。就像我在命令提示符中写一个命令一样,我通常按 Ctrl + C 来停止执行。
编辑:我有这个事件,当点击关闭按钮时触发。
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Int32 result = DllWrapper.HdsDestroy();
MessageBox.Show("Destroy result = " + (result == 0 ? "Success" : "Fail"));
}
我在 Internet 上找到了一个解决方案,SendKeys,但我无法理解。 PS:这可能是一些问题的重复,但其中一些对我不起作用。
提前致谢
【问题讨论】:
-
嘿@mjwills,我刚刚发现
process.Start()启动了两个进程,一个cmd.exe 和NodeJs,在process.Kill()上它只杀死了cmd.exe。所以服务器还在运行! -
是的。
startInfo.FileName = "http-server";使用这个。 -
在这种情况下,找到
nodejs进程并杀死它。
标签: c# windows command-line process