public InsomniaAppContext() { //See if application is already running on the system if (this.instancesAllowed.WaitOne(1000)) { // Initialize Tray Icon trayIcon = new NotifyIcon() { Icon = Properties.Resources.Insomnia, ContextMenu = new ContextMenu(new MenuItem[] { new MenuItem("Exit", Exit) }), Visible = true }; PowerHelper.ForceSystemAwake(); this.WasRunning = true; return; } else { //Display MessageBox.Show("An instance of Insomnia is already running"); //Exit out otherwise this.Exit(null, null); } }
void Exit(object sender, EventArgs e) { //Decrement the count if app was running if (this.WasRunning) { this.instancesAllowed.Release(); } // Hide tray icon, otherwise it will remain shown until user mouses over it trayIcon.Visible = false; PowerHelper.ResetSystemDefault(); Application.Exit(); }