【发布时间】:2014-12-01 05:01:17
【问题描述】:
我正在开发一个用 c# winforms 为应用程序编写的插件。 我已经在调试中设置了启动操作。我已经设置了启动外部程序,它正在启动该应用程序。 在我的 winforms 中,我为按钮单击事件编写了代码,该事件启动了一个新进程,例如 notepad.exe
现在的问题是,每当我关闭该外部应用程序时,从该按钮单击事件调用的所有打开的记事本都应该自动关闭。
如有任何帮助,我们将不胜感激。
【问题讨论】:
我正在开发一个用 c# winforms 为应用程序编写的插件。 我已经在调试中设置了启动操作。我已经设置了启动外部程序,它正在启动该应用程序。 在我的 winforms 中,我为按钮单击事件编写了代码,该事件启动了一个新进程,例如 notepad.exe
现在的问题是,每当我关闭该外部应用程序时,从该按钮单击事件调用的所有打开的记事本都应该自动关闭。
如有任何帮助,我们将不胜感激。
【问题讨论】:
您可以使用 System.Diagnostics.Process 启动进程,如下所示:
Process proc=Process.Start("notepad.exe");
在闭幕活动中,您可以使用:
proc.Kill;
您也可以只存储进程的 id:
//arr is int array where stored your processes ids
for(int i=0; i<arr.Length; i++)
{
Process.GetProcessById(arr[i]).Kill();
}
【讨论】:
在此示例中,我在关闭计算器时关闭记事本:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Process p in Process.GetProcessesByName("calc"))
{
p.EnableRaisingEvents = true;
p.Exited += p_Exited;
}
}
void p_Exited(object sender, EventArgs e)
{
foreach (Process p in Process.GetProcessesByName("notepad"))
{
p.CloseMainWindow();
}
}
【讨论】:
In my winforms I have written code for a button click event which starts a new process say notepad.exe ...所以用那个按钮将该代码放入表单中?当表单加载时,您连接外部应用程序的 Exited() 事件。这根本不涉及外部应用程序中的代码..