17

I'm using following code to iterate over a list of browsers executable paths and start each of them:

foreach (var browser in browsers)
{
    var proc = new Process();
    proc.StartInfo.FileName = browser.ExecutablePath;
    proc.StartInfo.Arguments = "http://google.com";
    proc.Start();
    proc.WaitForExit();
    Console.WriteLine(proc.ExitCode.ToString());
    proc.Close();
}

What is should do is: it should open browser window with google.com loaded and stop the application until the window is closed. And it works fine for both IE and Firefox, but fails with Chrome.

For Chrome proc is in Exit state just after launching the browser, when the window is still active and visible.

I tried using some of chromium command line switches, including --new-window and --single-process but with no success.

Question is, how can I force Google Chrome to run in the process it is started in, so it would be possible to wait until window is closed?

Update

Just to clarify the question:

  • I know why it does not work - it's because Chrome uses multiple processes for different things, like different tabs, plug-ins, etc.
  • I tried to find the correct process looking on process tree, but found nothing.
  • I can't just take the latest process created by chrome, because it may be the process created for a pluging the page requires, not the page itself.
4

3 回答 3

10

If you want to open/close entire Chrome window:

Chrome by default is constantly running in background because of its default settings. Change this option to unchecked:
Settings > Show advanced > System > 'Continue running background apps when Google Chrome is closed'

So you have to make sure Chrome is closed before you run it again by your code. That works for me.


If you want to open/close related tabs only:

Chrome have one 'mother process' and some child processes. The problem is that if you run chrome using your code you are trying to create new instance of 'mother process' but the existing one won't let you do it. She'll instantly kill your new process and create her own child instead. That's how it works...

So, all you need is figure out how to run another 'chrome mother process' and prevent the previous hug from killing her ;P

I figure out this solution:
Run new chrome process with this parameter --user-data-dir="%temp%/random_name". This means that you are opening chrome with new user profile.

Advantages:

  • It works
  • Chrome is opening in new window
  • Chrome is closing when all related tabs are closed

Disadvantages:

  • Default settings (no bookmarks, etc) but you can copy them from default user profile directory

So, maybe you should look for sth in this direction...

于 2013-10-26T23:21:26.853 回答
0

您可以找到您运行的 Chrome 进程的所有子进程,然后等待它们完成。

有一个 StackOverflow 问题有一些有用的代码:查找我自己的 .NET 进程的所有子进程/找出给定进程是否是我自己的子进程?. 您也可能会发现这很有用:Monitor child processes of a process

于 2013-10-30T17:46:27.037 回答
0

另一个(有点)起作用的命令行参数是--chrome-frame. 在此模式下,Chrome 似乎使用 WinInet API,因为 IE 历史记录可用。我更喜欢--user-data-dir@DamianDrygiel 提出的关于使用独特临时文件夹的想法。

于 2013-10-30T03:29:36.373 回答