public static void Main(string[] args) { bool newMutex; System.Threading.Mutex mutex = new System.Threading.Mutex(true, "{9F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}", out newMutex); // Attempt aquire the mutex if(newMutex) { // If we are able to aquire the mutex, it means the application is not running. Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // Create the new tray icon MyTray myTray = new MyTray(); Application.AddMessageFilter(myTray); Application.Run(); // Release the mutex on exit mutex.ReleaseMutex(); } else { // If the aquire attempt fails, the application is already running // so we broadcast a windows message to tell it to wake up. NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); } }
private static void Main(string[] args) { // Attempt aquire the mutex if (mutex.WaitOne(TimeSpan.Zero, true)) { // If we are able to aquire the mutex, it means the application is not running. Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // Create the new tray icon MyTray myTray = new MyTray(); Application.Run(); // Release the mutex on exit mutex.ReleaseMutex(); } else { // If the aquire attempt fails, the application is already running // so we broadcast a windows message to tell it to wake up. NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); } }