static void Main(string[] args) { SingleInstanceApp app; if (SingleInstanceApp.Aquire(out app)) { using (app) { var emacsWindow = FindWindow(EmacsClassName, null); if (args.Length == 0 && emacsWindow != IntPtr.Zero) {//No args, just want to run emacs //Just pop that window to the foreground if (IsIconic(emacsWindow)) { ShowWindow(emacsWindow, SW_RESTORE); } SetForegroundWindow(emacsWindow); } else { RunEmacsClient(args); } } } }
public static bool Aquire(Assembly assembly, out SingleInstanceApp app) { // get application GUID as defined in AssemblyInfo.cs string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly(). GetCustomAttributes(typeof(GuidAttribute), false). GetValue(0)).Value.ToString(); // unique id for global mutex - Global prefix means it is global to the machine string mutexId = string.Format("{{{0}}}", appGuid); // Need a place to store a return value in Mutex() constructor call bool createdNew; // edited by MasonGZhwiti to prevent race condition on security settings via VanNguyen var mutex = new Mutex(false, mutexId, out createdNew); // edited by acidzombie24 var hasHandle = false; try { try { hasHandle = mutex.WaitOne(0, false); } catch (AbandonedMutexException) { // Log the fact that the mutex was abandoned in another process, // it will still get acquired hasHandle = true; } if (hasHandle) { app = new SingleInstanceApp(mutex); return(true); } else { app = null; return(false); } } catch { if (hasHandle) { mutex.ReleaseMutex(); } app = null; return(false); } }
public static bool Aquire(out SingleInstanceApp app) { return(Aquire(Assembly.GetExecutingAssembly(), out app)); }