Singleton Application With NotifyIcon

I recently had to implement 2 changes to a winforms application I’m working on, here’s the general requirements:

  • Can only have 1 instance of the application running – make it a singleton application
  • The user can close (hide) the application but it remains running until exited – utilize the notify icon (tray icon)

I searched google and found a bunch of helpful sources for both, this is a melding of several of them to fit my needs.

Here are the guts of the winform that deals with the notifyIcon:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Windows.Forms;
 
namespace SingletonAppWithNotifyIcon
{
    public partial class MainForm2 : Form
    {
        private bool IsQuit { get; set; }
 
        public MainForm2()
        {
            InitializeComponent();
        }
 
        private void MyFormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsQuit)
                return;
 
            HideMe();
            e.Cancel = true;
        }
 
        private void HideMe()
        {
            Hide();
        }
 
        public void ShowMe()
        {
            WindowState = FormWindowState.Normal;
            Visible = true;
            Activate();
        }
 
        private void QuitMe()
        {
            IsQuit = true;
            Application.Exit();
        }
 
        private void MainForm2_Load(object sender, EventArgs e)
        {
            notifyIcon1.ContextMenu = new ContextMenu();
            notifyIcon1.ContextMenu.MenuItems.Add(new MenuItem("Quit My Fancy App", NotifyIconQuit));
        }
 
        private void NotifyIconQuit(object sender, EventArgs e)
        {
            QuitMe();
        }
 
        private void NotifyClick(object sender, EventArgs e)
        {
            ShowMe();
        }
 
        private void CloseClick(object sender, EventArgs e)
        {
            HideMe();
        }
 
        private void QuitClick(object sender, EventArgs e)
        {
            QuitMe();
        }
    }
}

Here the highlights for the singleton application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
 
namespace SingletonAppWithNotifyIcon
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
 
            SingleInstanceAppStarter.Start(new MainForm2(), StartNewInstance);
        }
 
        static void StartNewInstance(object sender, StartupNextInstanceEventArgs e)
        {
            var forms = Application.OpenForms;
            var frm = forms["MainForm2"] as MainForm2;
            if (frm != null)
            {
                frm.ShowMe();
            }
            else
            {
                var f = new MainForm2();
                f.ShowDialog();
            }
        }
    }
 
    class SingleInstanceApp : WindowsFormsApplicationBase
    {
        public SingleInstanceApp() { }
 
        public SingleInstanceApp(Form f)
        {
            IsSingleInstance = true;
            MainForm = f;
        }
    }
 
    public class SingleInstanceAppStarter
    {
        static SingleInstanceApp _app;
 
        public static void Start(Form f, StartupNextInstanceEventHandler handler)
        {
            if (_app == null && f != null)
            {
                _app = new SingleInstanceApp(f);
            }
 
            if (_app == null) return;
 
            _app.StartupNextInstance += handler;
            _app.Run(Environment.GetCommandLineArgs());
        }
    }	
}

Here’s a sample solution (nice and simple) for you to download and experiment with…
SingletonAppWithNotifyIcon.zip