Open Chrome from command line and wait till it's closed

后端 未结 3 2122
后悔当初
后悔当初 2021-02-13 01:29

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 Proc         


        
相关标签:
3条回答
  • 2021-02-13 02:11

    Another command line parameter that (sort of) works is --chrome-frame. It appears Chrome uses WinInet API when in this mode, because the IE history is available. I do like more the idea about using --user-data-dir with a unique temp folder, as proposed by @DamianDrygiel.

    0 讨论(0)
  • 2021-02-13 02:19

    You could find all the child processes of the Chrome process you run and then wait for them to finish.

    There is a StackOverflow question that has some useful code: Find all child processes of my own .NET process / find out if a given process is a child of my own?. Also you might find this useful: Monitor child processes of a process.

    0 讨论(0)
  • 2021-02-13 02:26

    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...

    0 讨论(0)
提交回复
热议问题