[RAPPS] Improve existing window re-use (#5617)

- Fix small race between CreateMutex and CMainWindow creating the window, and another instance getting NULL from FindWindow.
- If the existing window is minimized, it should be restored.
This commit is contained in:
Whindmar Saksit 2024-06-08 01:18:18 +02:00 committed by GitHub
parent 7e2cd98688
commit 3f6af8b848
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -355,15 +355,27 @@ ParseCmdAndExecute(LPWSTR lpCmdLine, BOOL bIsFirstLaunch, int nCmdShow)
if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS)) if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
{ {
/* If already started, find its window */ /* If already started, find its window */
HWND hWindow = FindWindowW(szWindowClass, NULL); HWND hWindow;
for (int wait = 2500, inter = 250; wait > 0; wait -= inter)
{
if ((hWindow = FindWindowW(szWindowClass, NULL)) != NULL)
break;
Sleep(inter);
}
/* Activate window */ if (hWindow)
ShowWindow(hWindow, SW_SHOWNORMAL); {
SetForegroundWindow(hWindow); /* Activate the window in the other instance */
SwitchToThisWindow(hWindow, TRUE);
if (bAppwizMode) if (bAppwizMode)
PostMessage(hWindow, WM_COMMAND, ID_ACTIVATE_APPWIZ, 0); PostMessage(hWindow, WM_COMMAND, ID_ACTIVATE_APPWIZ, 0);
if (hMutex)
CloseHandle(hMutex);
return FALSE; return FALSE;
} }
}
CMainWindow wnd(&db, bAppwizMode); CMainWindow wnd(&db, bAppwizMode);
MainWindowLoop(&wnd, nCmdShow); MainWindowLoop(&wnd, nCmdShow);