Skip to content

Commit

Permalink
adding single instance support
Browse files Browse the repository at this point in the history
  • Loading branch information
changbw001 committed Jan 3, 2017
1 parent 5597733 commit cc03297
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
18 changes: 10 additions & 8 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
//using System.Collections.Generic;
//using System.Configuration;
//using System.Data;
Expand All @@ -13,12 +14,20 @@ public partial class App : Application
public static MainWindow mainwin;
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}";

[System.Runtime.InteropServices.DllImport("shlwapi.dll")]
[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);
Current.Shutdown();
return;
}

AppDomain.CurrentDomain.AssemblyResolve += (object sender, ResolveEventArgs e) =>
{
var desiredAssembly = new System.Reflection.AssemblyName(e.Name).Name;
Expand Down Expand Up @@ -74,13 +83,6 @@ private void RunCheck(object sender1, StartupEventArgs e1)
return;
}

if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
{
MessageBox.Show((string)Resources["msgbox_one_inst"], "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
Current.Shutdown();
return;
}

mainwin = new MainWindow();
Current.MainWindow = mainwin;
mainwin.Show();
Expand Down
1 change: 1 addition & 0 deletions DesktopNote.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="SingleInstance.cs" />
<Compile Include="Win_Format.xaml.cs">
<DependentUpon>Win_Format.xaml</DependentUpon>
</Compile>
Expand Down
17 changes: 13 additions & 4 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public partial class MainWindow : Window
private object Lock_Save = new object();
private static int CountDown = 0;
Point mousepos;
private bool fbopen = false;


public MainWindow()
{
InitializeComponent();
Expand Down Expand Up @@ -249,7 +248,6 @@ private void RTB_Main_LostKeyboardFocus(object sender, KeyboardFocusChangedEvent

private void RTB_Main_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
fbopen = true;
e.Handled = true;
if (!RTB_Main.Selection.IsEmpty)
{
Expand Down Expand Up @@ -284,7 +282,6 @@ private void RTB_Main_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left && App.fb?.Opacity == 1)
{
fbopen = false;
App.fb.FadeOut();
}
}
Expand Down Expand Up @@ -510,6 +507,18 @@ private void Win_Main_Loaded(object sender, RoutedEventArgs e)
var task_save = new Thread(SaveNotes);
task_save.IsBackground = true;
task_save.Start();

var source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
source.AddHook(WndProc);
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == SingleInstance.RegisteredMsg)
{
MessageBox.Show("Body text", "Title text", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
return IntPtr.Zero;
}


Expand Down
2 changes: 1 addition & 1 deletion Resources/StringResources.en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Program will exit now.</sys:String>
<sys:String x:Key="tooltip_strikethrough">Strike-through (Ctrl + D)</sys:String>
<sys:String x:Key="tooltip_underline">Underline (Ctrl + U)</sys:String>
<sys:String x:Key="tooltip_undo">Undo (Ctrl + Z)</sys:String>
<sys:String x:Key="msgbox_one_inst">DesktopNote is already running.</sys:String>
<!--<sys:String x:Key="msgbox_one_inst">DesktopNote is already running.</sys:String>-->
<sys:String x:Key="menu_options">Options</sys:String>
<sys:String x:Key="label_content_path">Content path</sys:String>
<sys:String x:Key="label_content_path_txt">Text content path</sys:String>
Expand Down
2 changes: 1 addition & 1 deletion Resources/StringResources.zh.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<sys:String x:Key="tooltip_strikethrough">删除线 (Ctrl + D)</sys:String>
<sys:String x:Key="tooltip_underline">下划线 (Ctrl + U)</sys:String>
<sys:String x:Key="tooltip_undo">撤销 (Ctrl + Z)</sys:String>
<sys:String x:Key="msgbox_one_inst">DesktopNote已经在运行。</sys:String>
<!--<sys:String x:Key="msgbox_one_inst">DesktopNote已经在运行。</sys:String>-->
<sys:String x:Key="menu_options">选项</sys:String>
<sys:String x:Key="label_content_path">笔记保存路径</sys:String>
<sys:String x:Key="label_content_path_txt">文本保存路径</sys:String>
Expand Down
34 changes: 34 additions & 0 deletions SingleInstance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DesktopNote
{
class SingleInstance
{
public static readonly IntPtr HWND_BROADCAST = (IntPtr)0xffff;
public static readonly int RegisteredMsg = 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)]
public static extern bool SendNotifyMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int RegisterWindowMessage(string message);

public enum MutexScope { Local, Global }

/// <summary>
/// Return created mutex if the mutex name does not exist. Returns null if the mutex name exists.
/// </summary>
public static bool CheckExist(string uniquestr, MutexScope scope = MutexScope.Global)
{sdrsfsdf
bool createdNew;
new Mutex(true, scope.ToString() + @"\" + uniquestr, out createdNew);
return !createdNew;
}
}
}

0 comments on commit cc03297

Please sign in to comment.