From 2f1d270c08eb43f043e342accc64b1cd1bbff25f Mon Sep 17 00:00:00 2001 From: LePresidente Date: Tue, 31 Oct 2017 12:13:25 +0200 Subject: multiple fixes to waitForProcessCompletion. --- src/organizercore.cpp | 87 +++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/organizercore.cpp b/src/organizercore.cpp index 9ae8beae..cba92d8f 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -1265,48 +1265,53 @@ bool OrganizerCore::waitForProcessCompletion(HANDLE handle, LPDWORD exitCode) std::min(static_cast(maxCount), numProcesses); for (size_t i = 0; i < count; ++i) { std::wstring processName = getProcessName(processes[i]); - if (!boost::starts_with(processName, L"ModOrganizer.exe")){ - currentProcess = processes[i]; - m_UserInterface->setProcessName(QString::fromStdWString(processName)); - processHandle = ::OpenProcess(SYNCHRONIZE, FALSE, currentProcess); - found = true; - ::CloseHandle(processHandle); - } - } - if (!found) { - // it's possible the previous process has deregistered before - // the new one has registered, so we should try one more time - // with a little delay - if (tryAgain) { - tryAgain = false; - QThread::msleep(500); - continue; - } else { - break; - } - } else { - tryAgain = true; - } - // keep processing events so the app doesn't appear dead - - QCoreApplication::processEvents(); - - - if (exitCode != nullptr) { - //This is actually wrong if the process we started finished before we - //got the event and so we end up with a job handle. - if (! ::GetExitCodeProcess(processHandle, exitCode)) - { - DWORD error = ::GetLastError(); - qDebug() << "Failed to get process exit code: Error " << error; - } - } + if (!boost::starts_with(processName, L"ModOrganizer.exe")) { + currentProcess = processes[i]; + m_UserInterface->setProcessName(QString::fromStdWString(processName)); + processHandle = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, currentProcess); + found = true; + } + } + if (!found) { + // it's possible the previous process has deregistered before + // the new one has registered, so we should try one more time + // with a little delay + if (tryAgain) { + tryAgain = false; + QThread::msleep(500); + continue; + } + else { + break; + } + } + else { + tryAgain = true; + } + //Cleanup + if (processHandle != INVALID_HANDLE_VALUE) { + if (exitCode != nullptr) { + //This is actually wrong if the process we started finished before we + //got the event and so we end up with a job handle. + if (!::GetExitCodeProcess(processHandle, exitCode)) + { + DWORD error = ::GetLastError(); + qDebug() << "Failed to get process exit code: Error " << error; + } + } + if (handle != processHandle) { + ::CloseHandle(processHandle); + } + } - ::CloseHandle(processHandle); - if (handle != processHandle) { - ::CloseHandle(handle); - } - } + // keep processing events so the app doesn't appear dead + QCoreApplication::processEvents(); + } + //Final Cleanup + if (handle != INVALID_HANDLE_VALUE) { + ::CloseHandle(handle); + } + delete[] processes; return res == WAIT_OBJECT_0; } -- cgit v1.3.1