1

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.

trksyln
  • 136
  • 10
  • if the application doesn't have a window how is `MainWindowHandle` going to work? you will need to just hide your window instead of closing it, or use some other IPC method to communicate with it to open itself – TheGeneral Sep 27 '18 at 07:39
  • 1
    Also, take some time to format your code, its hard to read – TheGeneral Sep 27 '18 at 07:43
  • You could use a named mutex to check if your application is already running. Check: [Run single instance of an application using Mutex](https://stackoverflow.com/questions/819773/run-single-instance-of-an-application-using-mutex) – Jeroen van Langen Sep 27 '18 at 07:50
  • @Saruman it has window. it just hides and put a icon on tray icons to show it back. – trksyln Sep 27 '18 at 08:00
  • @J.vanLangen i'm using Regex but i didn't put it here to keep it more simple. so i detect if it is second instance. i just need to find a way to show other hidden instance in taskbar tray icons. – trksyln Sep 27 '18 at 08:02
  • @Saruman and about formatting, my code actually doesn't look like that. it's just more compact version where i struggle with but you are right i should do better for get help. – trksyln Sep 27 '18 at 08:18
  • It's usually better to arrange for some form of IPC between the programs. You can then `AllowSetForegroundWindow` on the other program and let *it* handle getting itself into the right visual state and making itself foreground. (You can also pass through any command line args passed to the second instance as well, once you've put IPC in place, if desired) – Damien_The_Unbeliever Sep 27 '18 at 08:34
  • @Damien_The_Unbeliever i though about that too, open a tcp or udp port and bring it back when any data received through it but this one seems easier and if i can manage to make it work ofc. :) thanks for advice anyway. – trksyln Sep 27 '18 at 08:42

0 Answers0