We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Activating existing instance of application when it''s in the system tray

Hello, I have posted this question before elsewhere but haven''t gotten any resolution. I have an application that we''ve written, that if it''s running and minmized in the system tray, and the application is ran again .. we are trying to activate the existing instance of the application. If the application is minimize to the task bar, the code bloew works fine, however if it is in the system tray, I can''t get it to maximize and become the active window. Any suggestions? I am pretty much dead in the water at this point. Here''s my code sample: Imports System.Threading Imports System.Diagnostics.Process Imports Microsoft.Win32 Imports System Public Sub Main() ''------------------------------------------------------------------------ '' Make sure that there are no other instances of my application running ''------------------------------------------------------------------------ Dim strCurrentProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName Dim intHandle As IntPtr Dim runningProc As Process Dim i As Integer Dim test Dim sWindowTitle as string = "MyApplication" If System.Diagnostics.Process.GetProcessesByName(strCurrentProcessName).Length > 1 Then For i = 0 To System.Diagnostics.Process.GetProcessesByName(strCurrentProcessName).Length - 1 If Process.GetProcessesByName(strCurrentProcessName)(i).Id <> Process.GetCurrentProcess().Id Then runningProc = Process.GetProcessesByName(strCurrentProcessName)(i) intHandle = runningProc.MainWindowHandle If IntPtr.Zero.Equals(intHandle) Then Debug.WriteLine("no window handle if minimized to tray (hidden, no main window)") Debug.WriteLine("we try the native Win32 find features") Debug.WriteLine("sWindowTitle=" & sWindowTitle) intHandle = FindWindow(Nothing, sWindowTitle) If IntPtr.Zero.Equals(intHandle) Then Debug.WriteLine("still no handle") Else Debug.WriteLine("finally have a handle") End If End If Debug.WriteLine("intHandle=" & intHandle.ToString) If IsIconic(intHandle.ToInt32) Then Debug.WriteLine("Was minimized in the taskbar.") System.Runtime.InteropServices.APIs.APIsUser32.ShowWindow(intHandle, Runtime.InteropServices.APIs.APIsEnums.ShowWindowStyles.RESTORE) Win32Helper.SetForegroundWindow(intHandle) Else '' This is where I can not get it to come out of the system tray Debug.WriteLine("not iconic") System.Runtime.InteropServices.APIs.APIsUser32.ShowWindow(intHandle, Runtime.InteropServices.APIs.APIsEnums.ShowWindowStyles.MAXIMIZE) Win32Helper.SetForegroundWindow(intHandle) End If End If Next Exit Sub End If ... ... ...

Loader.
Live Chat Icon For mobile
Up arrow icon