Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Dashboard/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
{
if (msg == NativeMethods.WM_SHOWMONITOR)
{
// Another instance tried to start - bring this window to front
// Another instance tried to start - bring this window to front (#769)
Show();
WindowState = WindowState.Normal;
if (WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Activate();
Topmost = true; // Temporarily set topmost to ensure visibility
Topmost = false;
Expand Down
28 changes: 23 additions & 5 deletions Lite/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ public partial class App : Application
[DllImport("shell32.dll", SetLastError = true)]
private static extern void SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string appId);

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindow(string? lpClassName, string lpWindowName);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);

private const int SW_RESTORE = 9;

private const string MutexName = "PerformanceMonitorLite_SingleInstance";
private Mutex? _singleInstanceMutex;
private bool _ownsMutex;
Expand Down Expand Up @@ -167,11 +182,14 @@ protected override void OnStartup(StartupEventArgs e)

if (!_ownsMutex)
{
MessageBox.Show(
"Performance Monitor Lite is already running.",
"Already Running",
MessageBoxButton.OK,
MessageBoxImage.Information);
/* Bring the existing instance's window to the foreground instead of showing an error (#769) */
var hWnd = FindWindow(null, "Performance Monitor Lite");
if (hWnd != IntPtr.Zero)
{
if (IsIconic(hWnd))
ShowWindow(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
}
Shutdown();
return;
}
Expand Down
Loading