C#在没有响应时关闭其他进程

时间:2018-02-26 02:35:41

标签: c#

我试图编写一个程序,当它没有响应时会重新启动另一个程序。当弹出并显示受监控程序未响应系统消息框时

 -> Check online for a solution or close the program
 -> Close the program
 -> Debug the program

并将Windows事件写入Windows事件日志。我为这个将运行重新启动程序的Windows事件添加一个触发器。它将杀死没有响应的程序然后重新启动它。但是我发现.kill()和.CloseMainWindow()都不能关闭不响应的程序和系统对话框。有没有办法杀死不响应的程序? 贝洛斯是我的代码:

           Process[] processes = Process.GetProcessesByName(app_name);

            foreach (Process p in processes)
            {
                bool havingMainWindowHandle = false;
                if (p.MainWindowHandle == IntPtr.Zero) {
                    havingMainWindowHandle = false;
                    writeLog(p.ProcessName + " don't have MainWindowHandle");
                } else {
                    havingMainWindowHandle = true;
                    writeLog(p.ProcessName + " status:" + p.Responding.ToString());
                }

                p.WaitForExit(1000);
                if (havingMainWindowHandle)
                {
                    p.CloseMainWindow();
                    p.Close();
                } else
                {
                    writeLog(app_name + " is terminated by .kill() API");
                    p.Kill(); //but function kill() can't terminate the not responding program 
                }
                Thread.Sleep(1000);
            }

            Process my_otherprogram = new Process();
            my_otherprogram.StartInfo.FileName = app_path;       
            my_otherprogram.Start();

0 个答案:

没有答案