简体   繁体   English

Windows 7中启动和终止进程的问题

[英]issue with starting and a killing a process in windows 7

i am trying to start a browser instance as a process from ac# code. 我正在尝试从ac#代码启动浏览器实例作为进程。 then i want to kill the same instance of the browser. 那么我想杀死浏览器的相同实例。 I tried finding the same instance with process id . 我尝试找到具有进程ID的相同实例。 But the process ids are different in task manager and the initial id which i got when i started the process. 但是任务管理器中的进程ID和启动进程时获得的初始ID不同。 what's the solution? 有什么解决方案? why is this happening? 为什么会这样呢? Development enviorment is windows 7. Windows 7是开发环境。

  int ID= 0;
  void Start()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe");
        startInfo.Arguments = "http://www.google.com";
        Process ieProcess = Process.Start(startInfo);
        ID= ieProcess.Id;
    }
  void Stop()
   {
    foreach (Process p in System.Diagnostics.Process.GetProcessesByName("iexplore"))
     {
       if ((p.Id == ID))
        {
                p.Kill();
        }
     }

This code will not work if IE is already launched. 如果已启动IE,则此代码将不起作用。 Close all IE browsers and then try to run the code. 关闭所有IE浏览器,然后尝试运行代码。 If it works then you may have to look for solution suggested in following link 如果有效,则可能必须在以下链接中寻找建议的解决方案

similar post- 类似的帖子
Process.kill() denied in Windows 7 32bits even with Administrator privileges 即使具有管理员权限,Windows 7 32位中的Process.kill()也被拒绝

Why don't you add your code to the question? 您为什么不将代码添加到问题中? It'll make life easy for the people who are interested in helping you. 对于有兴趣帮助您的人来说,这将使生活变得轻松。 If you get different PIDs, most probably there's something wrong with your code! 如果您获得不同的PID,则很可能是您的代码有问题! (I'm just guessing without seeing what you have tried.) (我只是猜测而未看到您尝试过的内容。)

Have a look at these questions as well. 也看看这些问题。
1) Getting PID of process started by Process.start() 1) 通过Process.start()开始获取进程的PID
2) Programmatically kill a process in vista/windows 7 in C# 2)以 编程方式在C#中的Vista / Windows 7中终止进程
3) Process.kill() denied in Windows 7 32bits even with Administrator privileges 3) 即使具有管理员权限,Windows 7 32位系统中也会拒绝Process.kill()



Adding the code makes it much easier to understand what the problem is and here's your problem. 添加代码使您更容易理解问题所在,这就是您的问题。

IE creates more than one process for one instance of the program. IE为该程序的一个实例创建多个进程。 ( more details about it ) That's why you get different PIDs (for the different processes). 有关它的更多详细信息 )这就是为什么您获得不同的PID(用于不同的进程)的原因。

What your code does is killing only one process of it (by the usage of if condition in the Stop() method!). 您的代码所做的只是杀死它的一个进程(通过在Stop()方法中使用if条件!)。 So the remaining process may generate InvalidOperationException when you try to execute Start() again(starting the same process)! 因此,当您再次尝试执行Start() (启动同一进程)时,其余进程可能会生成InvalidOperationException

So what your code should do is kill all the active iexplore processes. 因此,您的代码应该做的是杀死所有活动的iexplore进程。 This can be done by simply removing the if condition of Stop() method. 这可以通过简单地删除Stop()方法的if条件来完成。

foreach(Process p in Process.GetProcessesByName("iexplore"))
{
   p.Kill();
}

Let me know whether this worked. 让我知道这是否有效。

I have a similar issue, only I dont want to kill the IE process that I started, I want to bring it into focus. 我有一个类似的问题,只是我不想终止我开始的IE进程,我想让它成为焦点。 I have one app that starts 5 IE windows.(not tabs, but unique windows) I store the PIDs that I start each of the IE windows with. 我有一个启动5个IE窗口的应用程序。(不是选项卡,而是唯一的窗口)我存储了启动每个IE窗口的PID。 At particular times, I want to be able to: 在特定的时间,我希望能够:

  • select a PID, 选择一个PID
  • find the IE window related to that PID 查找与该PID相关的IE窗口
  • bring it into focus (minimizing the others) 集中精力(最大限度地减少其他因素)

This worked using XP and IE6 (required for the environment) Now when I am using Win 7 and IE 8, the PID that I stored is not found, and thus I no longer have the ability to change the window in focus. 这在XP和IE6(环境要求)下有效。现在,当我使用Win 7和IE 8时,找不到我存储的PID,因此我不再具有更改焦点窗口的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM