Show 1st instance of application when starting 2nd instance?
-
When my program starts, I want to check if it's already running. If it is running, I want to show it and bring it to the foreground. I know multiple ways of checking if my program is already running by using the Process class or Mutex class. However, my main problem comes when trying to get my program to show itself. I'm using a NotifyIcon and ShowInTaskBar is set to false. When my program is minimized to the system tray and the second process starts and uses the Process class to get the MainWindowHandle of the 1st process, it returns 0. I experimented with it and found that if I kept my program in the taskbar by setting ShowInTaskbar to true, it would have a non-zero MainWindowHandle I can use. But, I don't want it in the taskbar. So, how can reactivate my already running program if I try to start a 2nd instance of it when the MainWindowHandle is 0? I probably could save the window handle to a file (suck!) and use it when a second instance starts. Help please?
-
When my program starts, I want to check if it's already running. If it is running, I want to show it and bring it to the foreground. I know multiple ways of checking if my program is already running by using the Process class or Mutex class. However, my main problem comes when trying to get my program to show itself. I'm using a NotifyIcon and ShowInTaskBar is set to false. When my program is minimized to the system tray and the second process starts and uses the Process class to get the MainWindowHandle of the 1st process, it returns 0. I experimented with it and found that if I kept my program in the taskbar by setting ShowInTaskbar to true, it would have a non-zero MainWindowHandle I can use. But, I don't want it in the taskbar. So, how can reactivate my already running program if I try to start a 2nd instance of it when the MainWindowHandle is 0? I probably could save the window handle to a file (suck!) and use it when a second instance starts. Help please?
There are 2 ways I would go about this: The graceful way would be to use remoting. In your code that checks for a single instance you would set up remoting to communicate with the running instance of your program and call a method in that instance to bring itself to the foreground. Remoting is ideal for communicating locally across application and process boundaries. The only drawback is that you need a network card to avoid problems. In the case that you can't guarantee your client's will have a NIC you can use Windows API. You need to call the Activate Window API. Here's a quick intro into that API. http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=711