【问题标题】:C# - Kill process doesn't work properlyC# - 终止进程无法正常工作
【发布时间】:2015-09-02 16:36:58
【问题描述】:

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

这是我的启动过程。

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

【问题讨论】:

  • 您可能需要权限才能终止进程。
  • IE中的插件/扩展是否有可能像chrome一样有自己的进程?
  • 你确定你只启动一次吗?同一台机器上是否有很多用户同时打开 IE?
  • p = new Process(); 分配一个您从不使用的空进程。 p = Process.GetProcessById(p.Id); 用 p.Id 找到的进程覆盖 p,为什么不直接使用 p?正如@sirdank 指出的那样。如果您多次调用 start ,您将丢失之前对 p 的引用并且无法再关闭窗口
  • 在调试时我可以看到 p.Id 在我的系统上没问题。关闭它工作正常,但在客户端根本没有关闭。获取 processbyID 有什么问题吗?可能会弄错 id 吗?关闭/终止我启动的进程的最佳方法是什么?

标签: c# process kill-process


【解决方案1】:

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

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

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

【讨论】:

  • dispose 和 kill 有什么区别?以及如何杀死它真正的一个?
  • @orhun.begendi 我编辑了我的答案,包括对Dispose()Kill() 的一些澄清。我仍然认为你最大的问题是CloseMainWindow()static 修饰符。
【解决方案2】:

你可以试试这个:

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();
    }
}

查看thisthis 的答案。

再一次,如果你想在处理过程中杀死它——只需使用Kill 方法。你不必有 id、name 或其他任何东西。你有提到那个过程,对吧?这足以杀死它

【讨论】:

  • 它会杀死所有具有该名称的进程(Iexplore.exe)吗?我不想杀死所有使用此名称的进程,因为我的客户端使用 Iexplore.exe 上的某个页面来检查电子邮件等,我只需要杀死我打开的
  • @orhun.begendi 然后你必须摆脱foreach循环并通过id获取进程。使用这个:Process.GetProcessById
猜你喜欢
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 2022-07-22
  • 1970-01-01
相关资源
最近更新 更多