C# - 终止进程无法正常工作

C# - Kill process doesn't work properly

我正在开发 WPF 应用程序的某些部分。我正在为它创建多个用户控件。因此,我的用户控件之一,我用 url 打开 IExplore.exe。关闭该用户控件后,我关闭该进程。在我的系统上似乎工作正常,但使用我的用户控件的客户端机器似乎无法按我想要的方式工作。关闭 Iexplore.exe 是对的,但是当我查看 "Task Manager" 时,我看到太多 iexplore.exe 。其中一些 Iexplore.exe 无法关闭,因此使用此名称选项终止所有进程对我来说不是一个选项:)

这是我的 class 启动过程。

class ses_process
    {
        static Process p = new Process();
        private Process Proc;

        public static bool start(string url)
        {

            p = Process.Start("IEXPLORE.EXE", "-nomerge "+url);
            //startInfo.WindowStyle = ProcessWindowStyle.Maximized;
            return true;
        }
        public static bool stop()
        {
            p = Process.GetProcessById(p.Id);

            bool return_deger = p.CloseMainWindow();
            return return_deger;
        }
        public Process proc
        {
            get { return Proc; }
            set
            {
                if (Proc == null)
                {
                    Proc = p;
                }
            }
        }

    }

在任务管理器中看起来像这样;

iexplore.exe                 16524 Console                   10     20.268 K
iexplore.exe                 22572 Console                   10     40.636 K
iexplore.exe                  2356 Console                   10    109.452 K
calc.exe                     20572 Console                   10     11.208 K
wuauclt.exe                  11716 Console                   10      1.092 K
RuntimeBroker.exe             6096 Console                   10      3.180 K
iexplore.exe                 10660 Console                   10     16.536 K
iexplore.exe                 18272 Console                   10     71.972 K
iexplore.exe                 20996 Console                   10     15.004 K
iexplore.exe                 14188 Console                   10      2.080 K
iexplore.exe                 12664 Console                   10     15.120 K
iexplore.exe                  5612 Console                   10     27.660 K
iexplore.exe                 18772 Console                   10     15.572 K
iexplore.exe                 22568 Console                   10     35.944 K
iexplore.exe                 21796 Console                   10     14.852 K
iexplore.exe                 10524 Console                   10     19.100 K
iexplore.exe                 13984 Console                   10     14.808 K
iexplore.exe                 21088 Console                   10     19.664 K
iexplore.exe                 10856 Console                   10     14.008 K
iexplore.exe                  1048 Console                   10     12.652 K
iexplore.exe                 22236 Console                   10     15.428 K
iexplore.exe                 15584 Console                   10     24.204 K
iexplore.exe                 16248 Console                   10      6.116 K
iexplore.exe                  9684 Console                   10      3.064 K
iexplore.exe                   752 Console                   10     14.480 K
iexplore.exe                 12680 Console                   10     19.004 K
iexplore.exe                  5772 Console                   10     14.064 K
iexplore.exe                 17868 Console                   10     18.872 K
iexplore.exe                  9272 Console                   10      5.644 K
iexplore.exe                 14216 Console                   10      3.332 K
iexplore.exe                  6060 Console                   10     11.820 K
iexplore.exe                 21352 Console                   10     12.592 K
iexplore.exe                 19604 Console                   10     15.392 K
iexplore.exe                 16636 Console                   10     23.916 K
iexplore.exe                 12584 Console                   10     14.796 K
iexplore.exe                  2848 Console                   10     21.884 K
iexplore.exe                 13696 Console                   10      5.608 K
iexplore.exe                 11720 Console                   10      3.296 K
SearchProtocolHost.exe       20896 Console                   10      2.404 K

您尝试过使用 Process.Kill() 吗?我不确定 CloseMainWindow() 是否可以解决问题。我还建议在您完成的任何 Process 对象上调用 Dispose(),并从 p 中删除 static,就好像可能导致您泄漏 Process可能产生此行为的对象。

Dispose() 可能会调用 kill Kill() (我不确定)但我对此表示怀疑。对实现 IDisposable 的所有对象调用 Dispose() 是一个好习惯,因为它们使用某种类型的资源或其他应该释放的资源。这就是 using() {} 块的用途。然而,这一切都离题了。

我认为从 p 中删除 static 修饰符并调用 Kill() 而不是 CloseMainWindow() 将解决您的问题。

你可以试试这个:

Process[] processes = Process.GetProcessesByName("your_process");
foreach (Process process in processes)
{
    process.Kill();
    process.WaitForExit();
}

或者像这样:

Process process = Process.GetProcessById(12345678);
process.Kill();

如果需要,您当然可以添加 WaitForExit 方法。 Kill 方法是异步的,所以如果你想知道进程是否已经被杀死,你应该等待它,如果没有 - 只需调用 Kill 方法。有关详细信息,请查看 here

编辑: 如果您自己启动了该过程,则应使用以下代码: 基本上 Process 实现了 IDisposable 接口,你应该用 using 语法调用它,例如:

using(Process proc = CreateProcess())
{
    StartProcess(proc);
    if (!proc.WaitForExit(timeout))
    {
        proc.Kill();
    }
}

查看this and this答案。

再一次,如果你想在处理过程中终止它 - 只需使用 Kill 方法。您不必拥有 id、name 或其他任何内容。你有参考那个过程,对吧?这足以杀死它