Restore other instance of same application from tray when launched

287 views Asked by At

i created a program which hide itself to tray icons when minimized. and it can run only one instance and when you try to run it again it supposed to show and active the current instance. my code simply looks like this.

[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);

[STAThread]
static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);

  if(isRunningAlready())
   {
       string exe_name = AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "").Replace(".EXE", ""
       Process[] procList = Process.GetProcessesByName(exe_name);
       BringProcessToFront(procList)
       return;
   }
   Application.Run(new MainForm());
}


public static void BringProcessToFront(System.Diagnostics.Process[] processes)
{
   foreach (System.Diagnostics.Process process in processes)
   {
       IntPtr handle = process.MainWindowHandle;
       ShowWindow(handle, 1);
       SetForegroundWindow(handle);
   }
}

and this code works fine when window visible and inactive but when in program hidden in tray icons does nothing. what i am doing wrong? Thanks in advance.

0

There are 0 answers