7

I wrote a little WPF app that when 'closed' minimizes to the system tray (customer requirement). Double clicking pops it back up, or right click gives a context menu to exit.

But if the app is minimized, and the users navigate to Start->All Programs->The Application it starts a new instance.

What (in C#) do I need to do to get the app to maximize the running instance if the user does this rather than fire up a new instance?

Thanks!

Jason Down
  • 21,731
  • 12
  • 83
  • 117
Nicros
  • 5,031
  • 12
  • 57
  • 101
  • 2
    look up Mutex objects and grabbing a an active window. don't have the code in front of me, but it's the direction you need to take. -- I lied, try [this article on single instances of .net applications](http://www.ai.uga.edu/mc/SingleInstance.html) – Brad Christie Jan 06 '11 at 04:46
  • @Nicros, hey did you get any solution for this. – raghava arr Jun 23 '20 at 06:53

1 Answers1

7

This answer from Jon Skeet discusses using a mutex to do it

Mutex is the way to go. It's a lot less fragile than using process names etc.

However, you need to make sure the Mutex isn't garbage collected. In the case of a service (which is event driven rather than having a "main" method which runs to completion), the most sensible way of doing this is probably to put it in a static variable.

Dispose of the mutex when the service stops, so you don't need to wait for finalization or anything like that.

Matthew Brindley gives this example in the same question for his answer

[STAThread]
static void Main() 
{
   using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
   {
      if(!mutex.WaitOne(0, false))
      {
         MessageBox.Show("Instance already running");
         return;
      }

      Application.Run(new Form1());
   }
}

To maximize the other application you'll need to send it the message to maximize. See this article on message sending

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • Have to retract my Perfect! statement- this checks for the instance running and does not start a new one- but does not answer the question how to Maximize this system tray'ed application without starting a new instance. – Nicros Jan 07 '11 at 01:18
  • Pfft. Let me retract my retraction- missed the link at the bottom for messageing.. will check it out. – Nicros Jan 07 '11 at 01:20