Skip to content

Commit

Permalink
Merge pull request #75 from Sandip124/fix/multiple-instance
Browse files Browse the repository at this point in the history
fix: app not reopening  after exiting
  • Loading branch information
Sandip124 authored Jan 11, 2024
2 parents 1984d9d + f05fd1e commit 9303b65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
1 change: 0 additions & 1 deletion BatteryNotifier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
<Compile Include="Providers\FontProvider.cs" />
<Compile Include="Providers\ThemeProvider.cs" />
<Compile Include="Setting\appSetting.cs" />
<Compile Include="Setting\appSetting.Designer.cs" />
<Compile Include="Theming\BaseTheme.cs" />
<Compile Include="Theming\DarkTheme.cs" />
<Compile Include="Theming\LightTheme.cs" />
Expand Down
33 changes: 14 additions & 19 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Squirrel;
using BatteryNotifier.Helpers;
using System.Threading;
using System.Runtime.InteropServices;

namespace BatteryNotifier
{
Expand All @@ -17,12 +16,7 @@ internal static class Program

private static string? version = UtilityHelper.AssemblyVersion;

[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
private static readonly string EventName = "BatteryNotifier.Instance.WaitHandle";

/// <summary>
/// The main entry point for the application.
Expand All @@ -37,18 +31,13 @@ static void Main()
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

System.Diagnostics.Process[] name = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);

if (name.Length > 1)
{
ShowWindowAsync(name[0].MainWindowHandle, WS_SHOWNORMAL);
SetForegroundWindow(name[0].MainWindowHandle);
}
else
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out bool createdNew))
{
if (createdNew)
{

MainForm = new Dashboard();
var dashboard = MainForm as Dashboard;
MainForm = new Dashboard();
var dashboard = MainForm as Dashboard;
#if RELEASE

if (InternetConnectivityHelper.CheckForInternetConnection())
Expand All @@ -60,10 +49,16 @@ static void Main()
version = UpdateManager?.CurrentlyInstalledVersion().ToString();
}
#endif
dashboard?.SetVersion(version);
dashboard?.SetVersion(version);

Application.Run(dashboard);
Application.Run(dashboard);
}
else
{
(MainForm as Dashboard)?.Notify("Another instance of Battery Notifier is already running.");
}
}

}
catch (Exception e)
{
Expand Down

0 comments on commit 9303b65

Please sign in to comment.