Skip to content

Commit 73288e0

Browse files
committed
CheckRunningInstances fix
1 parent 84d1a15 commit 73288e0

1 file changed

Lines changed: 7 additions & 22 deletions

File tree

OpenHardwareMonitor/Program.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
using System;
22
using System.Diagnostics;
33
using System.Linq;
4-
using System.Threading;
54
using System.Windows.Forms;
65
using OpenHardwareMonitor.UI;
76

87
namespace OpenHardwareMonitor;
98

109
public static class Program
1110
{
12-
static Mutex mutex = new Mutex(true, "{3661aef2-95dc-433e-932c-2e06112d24ec}");
13-
1411
[STAThread]
1512
public static void Main()
1613
{
14+
Crasher.Listen();
15+
1716
if (!OperatingSystemHelper.IsCompatible(false, out var errorMessage, out var fixAction))
1817
{
1918
if (fixAction != null)
@@ -30,27 +29,14 @@ public static void Main()
3029
Environment.Exit(0);
3130
}
3231

33-
if (!mutex.WaitOne(TimeSpan.Zero, true))
32+
if (WinApiHelper.CheckRunningInstances(true, true))
3433
{
35-
var process = Process.GetProcessesByName(Updater.ApplicationName).FirstOrDefault();
36-
if (process != null)
37-
{
38-
process.Refresh();
39-
IntPtr hwnd = process.MainWindowHandle;
40-
if (hwnd == IntPtr.Zero)
41-
hwnd = NativeMethods.FindWindow(null, Updater.ApplicationTitle);
42-
if (hwnd != IntPtr.Zero)
43-
{
44-
NativeMethods.SendMessage(hwnd, NativeMethods.WM_USER + 1, 0, IntPtr.Zero);
45-
Environment.Exit(0);
46-
}
47-
}
48-
MessageBox.Show("Another instance of the application is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
49-
Environment.Exit(0);
34+
// fallback
35+
MessageBox.Show($"{Updater.ApplicationName} is already running.", Updater.ApplicationName,
36+
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
37+
return;
5038
}
5139

52-
Crasher.Listen();
53-
5440
Application.EnableVisualStyles();
5541
Application.SetCompatibleTextRenderingDefault(false);
5642
using (MainForm form = new MainForm())
@@ -60,7 +46,6 @@ public static void Main()
6046
Application.Exit();
6147
};
6248
Application.Run();
63-
mutex.ReleaseMutex();
6449
}
6550
}
6651
}

0 commit comments

Comments
 (0)