1

I have written a console application in C# and it opens up a browser on the following command.

Process webprocess =  Process.Start("http://www.hashgurus.com");

However after the work finishes i want to close the browser. I am trying to kill the process but it doesnot work.

webprocess.Kill();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Venkat
  • 1,702
  • 2
  • 27
  • 47
  • Have you tried [Kill](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill.aspx) – NASSER Sep 20 '15 at 03:12
  • 2
    Very large processes, like a browser, generally work as a single-instance app. In other words, if the browser is already running and you run this code, the browser simply asks the first instance to display the web page and terminates. One way to tell is seeing the page opening in another tab. Kill() cannot work, the process is already terminated. – Hans Passant Sep 20 '15 at 07:22

2 Answers2

0
Process webBrowser = Process.Start( url );
webBrowser.WaitForExit();
webBrowser.Kill();
webBrowser.WaitForExit();

Note I don't know how this works with multi-process browsers like most modern web-browsers are. If you want to be sure, use a recursive kill-all-child-processes method to ensure all child processes in the tree are killed. However I believe child-process processes in a multi-process browser detect if their parent is killed and then commit ritual seppuku in that situation.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • This doesnot work. Process webprocess = Process.Start(url); When i debug I get Webprocess = null – Venkat Sep 20 '15 at 03:25
0

You can try to get a list of all processes and iterate through them and kill the ones you want. You will need to use System.Diagnostics.

Heres some steps of how it would work:

  1. make array of processes
  2. set the items in the array to all running processes (i think with diagnostics there is something like GetProcesses() method)
  3. for each process in the array
  4. check the title of the window (I think it's .MainWindowTitle)
  5. kill the process if it is the browser you want to kill (like check if the process name is "chrome", if so then kill
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Adam Wolf
  • 309
  • 1
  • 4
  • 12