0

I have the below code to check if 'chrome' is running when I click Button1. If not it launches chrome. This works but I dont know the code needed in the If statement to switch to the chrome if its already running. Hopefully this is something very simple i am missing.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Process.GetProcessesByName("chrome").Count > 0 Then ??**SHOW RUNNING APPLICATION**?? Else Process.Start("C:\Program Files\Google\Chrome\Application\chrome.exe") End If End Sub 
2
  • 3
    Chrome start's many instances of itself. Each tab has it's own process, so how are you going to tell it which one to switch to? Commented Jul 7, 2015 at 20:30
  • This is not an issue. There will only be one tab running at a given time. Commented Jul 8, 2015 at 7:31

1 Answer 1

4

As I mentioned in my above comment, Chrome starts many instances of itself. Each tab has its own process, so how are you going to tell it which one to switch to?. This come's down to what tab was selected when you minimize the window or it minimizes itself to the task bar. Below should help you out and it's tried and tested. The only issue is, if you open Chrome and have multiple tabs it's fine, but if you create another instance of Chrome it will not show the second instance, it will only bring forward the first instance... If you close the first instance, the second instance of course will come forward.

Public Class Form1 #Region "DLL Imports" <System.Runtime.InteropServices.DllImport("User32.dll")> _ Private Shared Function SetForegroundWindow(handle As IntPtr) As Boolean End Function <System.Runtime.InteropServices.DllImport("User32.dll")> _ Private Shared Function ShowWindow(handle As IntPtr, nCmdShow As Integer) As Boolean End Function <System.Runtime.InteropServices.DllImport("User32.dll")> _ Private Shared Function IsIconic(handle As IntPtr) As Boolean End Function #End Region Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click StartOrShowProcess("chrome") End Sub Private Sub StartOrShowProcess(ByVal strProcessName As String) Try Dim handle As IntPtr Dim proc As Process() = Process.GetProcessesByName(strProcessName) If proc.Count > 0 Then For Each procP As Process In proc handle = procP.MainWindowHandle If handle <> 0 AndAlso IsIconic(handle) Then 'Do we have a handle and is it minimized? ShowWindow(handle, 9) SetForegroundWindow(handle) End If Next Else 'Not running or started... Process.Start(strProcessName) End If Catch ex As Exception 'Handle your error... End Try End Sub End Class 
Sign up to request clarification or add additional context in comments.

2 Comments

This worked great thankyou! However it will only bring a window to the front if its been minimized. If its in the background then nothing happens. Can the be amended to it always brings the window forward?
@TomLumbardܤ yes it can come to front of the order if it isn't. Just remove this AndAlso IsIconic(handle) . This will then not matter if the window is minimized or not and or if it's not in the front.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.