Skip to content

Commit

Permalink
improve single instance support
Browse files Browse the repository at this point in the history
  • Loading branch information
changbw001 committed Jan 4, 2017
1 parent cc03297 commit f25c9e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
14 changes: 10 additions & 4 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -88,6 +89,11 @@ private void RunCheck(object sender1, StartupEventArgs e1)
mainwin.Show();
}


protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
mtx?.Close();
}

}
}
8 changes: 4 additions & 4 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
16 changes: 11 additions & 5 deletions SingleInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -22,12 +24,16 @@ class SingleInstance
public enum MutexScope { Local, Global }

/// <summary>
/// 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.
/// </summary>
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;
}
}
Expand Down
Binary file modified bin/Release/DesktopNote.exe
Binary file not shown.

0 comments on commit f25c9e8

Please sign in to comment.