Trouble closing Google Chrome browser from within VB.net

RyanH11
RyanH11 used Ask the Experts™
on
I have an automation program that I'm developing that is doing most of what I want it to do.  I can open various web browsers without a problem.

When it comes to closing them, Firefox, Opera and Safari all close fine using the code

 
Dim p As Process() = Process.GetProcessesByName(SelectedBrowserName)
            For i = 0 To p.Count - 1
                        p(i).CloseMainWindow()
                        Do Until p(i).HasExited = True
                            Thread.Sleep(100)
                        Loop
                    Next

Open in new window


Google Chrome however doesn't respond at all.  The only way I can get it to close is to use the 'kill' command which causes some grief when I next load up Chrome.  

I'm not 100% certain, but I'm pretty sure it used to work fine and has only started doing this in recent updates.  Can anybody offer a suggestion that reliably works with the current version of Chrome?

Cheers
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
That is a little quirky...it required multiple passes on my system...

This worked for me:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SelectedBrowserName As String = "Chrome"

        Dim ps() As Process = Process.GetProcessesByName(SelectedBrowserName)
        While ps.Length > 0
            For i As Integer = ps.Length - 1 To 0 Step -1
                ps(i).CloseMainWindow()
            Next

            ps = Process.GetProcessesByName(SelectedBrowserName)
        End While
    End Sub

Open in new window

Top Expert 2010

Commented:
Each process is associated with a tab it's possible the processes don't have a MainWindowHandle so for each process instance it means the MainWindowHandle could be zero. You might be able to filter by checking if MainWindowHandle of the process is greater then zero then use .CloseMainWindow

Author

Commented:
It has worked properly in all testing to date.  Many thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial