日期:2014-05-19  浏览次数:20745 次

窗口关闭后如何关闭主进程?
我用了一个进程来接收数据,显示在主窗口上,但是关闭了主窗口,主进程始终存在,下一次打开后就报错.必须在任务管理器里关掉主进程.

请问各位高手:怎么样才能在闭窗口的时候关闭主进程?
试过把托管进程设置为后台进程,即在main中设置                       Thread.CurrentThread.IsBackground   =   true;
不管用

请问还有什么方法可以?

------解决方案--------------------
在窗体的closing事件中关闭线程
------解决方案--------------------
protected static void KillProcess(string processName)
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
//得到所有打开的进程
try
{
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
{
if (!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch (Exception Exc)
{
throw new Exception( " ", Exc);
}
}