diff --git a/App.xaml.cs b/App.xaml.cs index 3caaa73..b5eab62 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -15,15 +15,16 @@ public partial class App : Application public static Win_Format fb; internal const int MaxWindowCount = 2;//need to set this to 4 while debugging if you use live debug toolbar in vs2015. public const string MutexString = @"{39622D35-176E-453D-B1FD-E4EC1EAF31DC}"; + private System.Threading.Mutex mtx; [DllImport("shlwapi.dll")] private static extern bool PathIsNetworkPath(string pszPath); private void RunCheck(object sender1, StartupEventArgs e1) { - if (SingleInstance.CheckExist(MutexString)) - {asdfasfasdf - SingleInstance.SendNotifyMessage(SingleInstance.HWND_BROADCAST, SingleInstance.RegisteredMsg, IntPtr.Zero, IntPtr.Zero); + if (SingleInstance.CheckExist(MutexString, ref mtx)) + { + SingleInstance.SendNotifyMessage(SingleInstance.HWND_BROADCAST, SingleInstance.RegisteredWM, IntPtr.Zero, IntPtr.Zero); Current.Shutdown(); return; } @@ -88,6 +89,11 @@ private void RunCheck(object sender1, StartupEventArgs e1) mainwin.Show(); } - + protected override void OnExit(ExitEventArgs e) + { + base.OnExit(e); + mtx?.Close(); + } + } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index f59164a..982e95a 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -18,7 +18,7 @@ public partial class MainWindow : Window private object Lock_Save = new object(); private static int CountDown = 0; Point mousepos; - + public MainWindow() { InitializeComponent(); @@ -501,7 +501,6 @@ private void Win_Main_Loaded(object sender, RoutedEventArgs e) RTB_Main.IsUndoEnabled = true; //without the above two lines, Load actions can be undone. - currScrnRect = new GetCurrentMonitor().GetInfo(); var task_save = new Thread(SaveNotes); @@ -514,9 +513,10 @@ private void Win_Main_Loaded(object sender, RoutedEventArgs e) private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { - if (msg == SingleInstance.RegisteredMsg) + if (msg == SingleInstance.RegisteredWM) { - MessageBox.Show("Body text", "Title text", MessageBoxButton.OK, MessageBoxImage.Exclamation); + Activate(); + UnDock(); } return IntPtr.Zero; } diff --git a/SingleInstance.cs b/SingleInstance.cs index a7684be..2736e3b 100644 --- a/SingleInstance.cs +++ b/SingleInstance.cs @@ -11,7 +11,9 @@ namespace DesktopNote class SingleInstance { public static readonly IntPtr HWND_BROADCAST = (IntPtr)0xffff; - public static readonly int RegisteredMsg = RegisterWindowMessage("WM_SHOW_DESKTOPNOTE"); + //from MSDN: If two different applications register the same message string, the applications return the same message value. + //The message remains registered until the session ends. + public static readonly int RegisteredWM = RegisterWindowMessage("WM_SHOW_DESKTOPNOTE"); //[DllImport("user32")] //not working when ShowInTaskbar set to false. //public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam); [DllImport("user32.dll", CharSet = CharSet.Auto)] @@ -22,12 +24,16 @@ class SingleInstance public enum MutexScope { Local, Global } /// - /// Return created mutex if the mutex name does not exist. Returns null if the mutex name exists. + /// Return false and the created mutex if the mutex name does not exist. Otherwise returns true and null. /// - public static bool CheckExist(string uniquestr, MutexScope scope = MutexScope.Global) - {sdrsfsdf + public static bool CheckExist(string uniquestr, ref Mutex mtx, MutexScope scope = MutexScope.Global) + { bool createdNew; - new Mutex(true, scope.ToString() + @"\" + uniquestr, out createdNew); + var newmtx = new Mutex(false, scope.ToString() + @"\" + uniquestr, out createdNew); + if (createdNew) + mtx = newmtx; + else + newmtx.Close(); return !createdNew; } } diff --git a/bin/Release/DesktopNote.exe b/bin/Release/DesktopNote.exe index 9098b04..d3fdbe7 100644 Binary files a/bin/Release/DesktopNote.exe and b/bin/Release/DesktopNote.exe differ