2015-06-17 45 views
-1

我正在开发WPF应用程序的某些部分。我为它创建了多个用户控件。所以,我的一个用户控件,我打开IExplore.exe带有一个URL。关闭该用户控件后,我关闭该进程。在我的系统上似乎工作正常,但使用我的用户控件的客户端机器似乎不像我想要的那样工作。关闭Iexplore.exe这是正确的,但当我看着“任务管理器”,我看到太多iexplore.exe。有些Iexplore.exe无法关闭,所以使用此名称选项杀死所有进程选项对我来说不是一种选择:)C# - 杀死进程无法正常工作

这是我的启动过程类。

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 
+0

您可能需要权限才能终止该进程。 – Lloyd

+0

IE中的插件/扩展有可能像Chrome一样拥有自己的进程吗? –

+0

你确定你只开始一次吗?同一台机器上有多少用户同时打开IE? – sirdank

回答

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方法。欲了解更多详细看here

编辑: 如果您自行启动的进程,您应使用此代码: 基本上Process实现IDisposable接口,你应该using语法例如叫它:

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

看看thisthis的答案。

再次,如果你想在处理过程中杀死它 - 只需使用Kill方法。你不必拥有身份证,姓名等等。你有参考这个过程,对吗?这足以杀死它

+0

是否用该名称(Iexplore.exe)终止所有进程?我不想用这个名字杀死所有进程,因为我的客户端在Iexplore.exe上使用了一些页面来检查电子邮件等,我只需要杀死我就打开 –

+0

@ orhun.begendi然后你必须摆脱foreach循环,通过ID获取进程。使用这个:Process.GetProcessById – voytek

0

您是否尝试过使用Process.Kill()代替?我不确定CloseMainWindow()会诀窍。我还建议在您完成的任何Process对象上拨打Dispose(),并从p中删除static,好像可能导致您泄漏Process可能产生此行为的对象。

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

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

+0

dispose和kill有什么区别?而如何杀死它真的吗? –

+0

@ orhun.begendi我编辑了我的答案,包括一些关于'Dispose()'和'Kill()'的说明。我仍然认为你最大的问题是'CloseMainWindow()'和'static'修饰符。 – sirdank