Skip to content

Commit 9303b65

Browse files
authored
Merge pull request #75 from Sandip124/fix/multiple-instance
fix: app not reopening after exiting
2 parents 1984d9d + f05fd1e commit 9303b65

2 files changed

Lines changed: 14 additions & 20 deletions

File tree

BatteryNotifier.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
<Compile Include="Providers\FontProvider.cs" />
8787
<Compile Include="Providers\ThemeProvider.cs" />
8888
<Compile Include="Setting\appSetting.cs" />
89-
<Compile Include="Setting\appSetting.Designer.cs" />
9089
<Compile Include="Theming\BaseTheme.cs" />
9190
<Compile Include="Theming\DarkTheme.cs" />
9291
<Compile Include="Theming\LightTheme.cs" />

Program.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Squirrel;
66
using BatteryNotifier.Helpers;
77
using System.Threading;
8-
using System.Runtime.InteropServices;
98

109
namespace 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

Comments
 (0)