1

Im using Visual studio 2012, windows form. I need restore application from system tray when I click shortcut on desktop.

Im using this code that prevent double istance of same application, but not open application from system tray.

[STAThread]
    static void Main()
    {
        string mutex_id = "my application";
        using (Mutex mutex = new Mutex(false, mutex_id))
        {
            if (!mutex.WaitOne(0, false))
            {
                Form1 fForm;
                fForm = new Form1();
                fForm.Show();
                fForm.WindowState = FormWindowState.Normal;
                fForm.ShowInTaskbar = true;
                // MessageBox.Show("Instance Already Running!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

I readed some questions here in stackoverflow, without luck. What I need modify in this code? thanks in advance!

Vincenzo Lo Palo
  • 1,341
  • 5
  • 19
  • 32

2 Answers2

2

You could use Mutex to accomplish this. Here is a bolt-on solution from CodePlex, that is very easy to incorporated into your program. The solution does provides the following features:

  • If the First Instance is Minimized to the System Tray (aka "Notification Area"), Restore It
  • Activating the First Instance
  • Prevent a Second Instance from Opening
FrostyFire
  • 3,212
  • 3
  • 29
  • 53
0

Check this question and answers with different ideas on getting instance of app that is already running. And check this link - http://msdn.microsoft.com/en-us/magazine/cc163741.aspx (look fo mutex).

Community
  • 1
  • 1
Konrad Gadzina
  • 3,407
  • 1
  • 18
  • 29