/// <summary> /// The first method that should be called when using SingletonApplication. /// </summary> /// <param name="args">Pass the startup arguments to this.</param> /// <param name="sharedID">Can be anything you choose, must be the same in both instances.</param> /// <returns>Returns true if it is the first instance otherwise returns false.</returns> public static bool Instance(string[] args, string sharedID) { appID = sharedID; _instance = new SingletonApplication(); SetArgs(args); bool owned = false; myMutex = new Mutex(true, sharedID, out owned); GC.KeepAlive(myMutex); if (owned) { _instance.sw = new SpecialNativeWindow(sharedID); _instance.sw.ApplicationOpened += new EventHandler<EventArgs>(sw_ApplicationOpened); GC.KeepAlive(_instance); GC.KeepAlive(_instance.sw); } else { IntPtr foundWin = FindWindowByCaption(0, sharedID); SendNotifyMessage(foundWin, CMESSAGE, IntPtr.Zero, IntPtr.Zero); } return owned; }
public void Dispose() { if (myMutex != null) { myMutex.ReleaseMutex(); } if (_instance.sw != null) { _instance.sw.DestroyHandle(); _instance.sw = null; } _instance = null; }