Search code examples
c#system-tray

Restore program from system tray clicking shortcuts in desktop in c#


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!


Solution

  • 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