55using Squirrel ;
66using BatteryNotifier . Helpers ;
77using System . Threading ;
8- using System . Runtime . InteropServices ;
98
109namespace BatteryNotifier
1110{
@@ -17,12 +16,7 @@ internal static class Program
1716
1817 private static string ? version = UtilityHelper . AssemblyVersion ;
1918
20- [ DllImport ( "User32.dll" ) ]
21- private static extern bool ShowWindowAsync ( IntPtr hWnd , int cmdShow ) ;
22-
23- [ DllImport ( "User32.dll" ) ]
24- private static extern bool SetForegroundWindow ( IntPtr hWnd ) ;
25- private const int WS_SHOWNORMAL = 1 ;
19+ private static readonly string EventName = "BatteryNotifier.Instance.WaitHandle" ;
2620
2721 /// <summary>
2822 /// The main entry point for the application.
@@ -37,18 +31,13 @@ static void Main()
3731 Application . EnableVisualStyles ( ) ;
3832 Application . SetCompatibleTextRenderingDefault ( false ) ;
3933
40- System . Diagnostics . Process [ ] name = System . Diagnostics . Process . GetProcessesByName ( System . Diagnostics . Process . GetCurrentProcess ( ) . ProcessName ) ;
41-
42- if ( name . Length > 1 )
43- {
44- ShowWindowAsync ( name [ 0 ] . MainWindowHandle , WS_SHOWNORMAL ) ;
45- SetForegroundWindow ( name [ 0 ] . MainWindowHandle ) ;
46- }
47- else
34+ using ( EventWaitHandle eventWaitHandle = new EventWaitHandle ( false , EventResetMode . AutoReset , EventName , out bool createdNew ) )
4835 {
36+ if ( createdNew )
37+ {
4938
50- MainForm = new Dashboard ( ) ;
51- var dashboard = MainForm as Dashboard ;
39+ MainForm = new Dashboard ( ) ;
40+ var dashboard = MainForm as Dashboard ;
5241#if RELEASE
5342
5443 if ( InternetConnectivityHelper . CheckForInternetConnection ( ) )
@@ -60,10 +49,16 @@ static void Main()
6049 version = UpdateManager ? . CurrentlyInstalledVersion ( ) . ToString ( ) ;
6150 }
6251#endif
63- dashboard ? . SetVersion ( version ) ;
52+ dashboard ? . SetVersion ( version ) ;
6453
65- Application . Run ( dashboard ) ;
54+ Application . Run ( dashboard ) ;
55+ }
56+ else
57+ {
58+ ( MainForm as Dashboard ) ? . Notify ( "Another instance of Battery Notifier is already running." ) ;
59+ }
6660 }
61+
6762 }
6863 catch ( Exception e )
6964 {
0 commit comments