0

i am trying to start a browser instance as a process from a c# code. then i want to kill the same instance of the browser. I tried finding the same instance with process id . But the process ids are different in task manager and the initial id which i got when i started the process. what's the solution? why is this happening? Development enviorment is 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();
        }
     }
user1687824
  • 807
  • 2
  • 10
  • 24
  • afaik when you launch Internet explorer via this method if one is already launched it will send a Open another page message to the original one so the process you launched will open and then close. although i may be wrong – Paul Farry Dec 06 '12 at 06:59
  • 1
    Don't do it this way. Use the automation interface. Then you can just call `Quit` to close the IE window. – Raymond Chen Dec 06 '12 at 07:35
  • @user1687824 I've edited my answer. Check it out. – CRoshanLG Dec 06 '12 at 07:44
  • @RaymondChen can you share some link showing how to do that? it will be of great help – user1687824 Dec 11 '12 at 05:11
  • [Here's a C++ version](http://msdn.microsoft.com/library/aa752127.aspx). [Here's a scripting version](http://blogs.msdn.com/b/oldnewthing/archive/2011/11/18/10238335.aspx). Translating them to C# is [left as an exercise](http://stackoverflow.com/questions/960032/close-application-from-captive-ie-session). – Raymond Chen Dec 11 '12 at 15:24

3 Answers3

1

This code will not work if IE is already launched. Close all IE browsers and then try to run the code. 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

Community
  • 1
  • 1
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
0

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! (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()
2) Programmatically kill a process in vista/windows 7 in C#
3) Process.kill() denied in Windows 7 32bits even with Administrator privileges



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. (more details about it) That's why you get different PIDs (for the different processes).

What your code does is killing only one process of it (by the usage of if condition in the Stop() method!). So the remaining process may generate InvalidOperationException when you try to execute Start() again(starting the same process)!

So what your code should do is kill all the active iexplore processes. This can be done by simply removing the if condition of Stop() method.

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

Let me know whether this worked.

Community
  • 1
  • 1
CRoshanLG
  • 498
  • 1
  • 8
  • 20
  • thanks for the reply.But the problem is i dont want other tabs to be closed. I just want to launch one link and after some time i want to close it. is there any way where i can store the alreay opened instances and launch again ? – user1687824 Dec 06 '12 at 08:42
  • In that case you will have to know the PID(s) of the tab(s) you need to close! I don't think it's possible to get the PID of specific tab! – CRoshanLG Dec 06 '12 at 09:52
0

I have a similar issue, only I dont want to kill the IE process that I started, I want to bring it into focus. 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. At particular times, I want to be able to:

  • select a PID,
  • find the IE window related to that PID
  • 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.