1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +02:00

Two-pass RunAndWait

This commit is contained in:
Brian Fiete 2024-10-21 10:51:02 -04:00
parent ad2aa15e42
commit bf9272377d
3 changed files with 50 additions and 41 deletions

View file

@ -120,50 +120,59 @@ int main()
std::string cmdLine = useCmdLineStr;
PROCESS_INFORMATION processInfo;
STARTUPINFOA si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
memset(&processInfo, 0, sizeof(processInfo));
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
HANDLE stdOut;
CreatePipe(stdOut, si.hStdOutput, false);
HANDLE stdErr;
CreatePipe(stdErr, si.hStdError, false);
si.dwFlags = STARTF_USESTDHANDLES;
DWORD startTick = GetTickCount();
BOOL worked = CreateProcessA(NULL, (char*)cmdLine.c_str(), NULL, NULL, TRUE,
flags, envPtr, NULL, &si, &processInfo);
::CloseHandle(si.hStdOutput);
::CloseHandle(si.hStdError);
if (!worked)
return 1;
DWORD threadId;
ProcParams stdOutParams = { stdOut, GetStdHandle(STD_OUTPUT_HANDLE) };
HANDLE stdOutThread = ::CreateThread(NULL, (SIZE_T)128*1024, (LPTHREAD_START_ROUTINE)ReadProc, (void*)&stdOutParams, 0, &threadId);
ProcParams stdErrParams = { stdErr, GetStdHandle(STD_ERROR_HANDLE) };
HANDLE stdErrThread = ::CreateThread(NULL, (SIZE_T)128 * 1024, (LPTHREAD_START_ROUTINE)ReadProc, (void*)&stdErrParams, 0, &threadId);
while (true)
for (int pass = 0; pass < 2; pass++)
{
if (::WaitForSingleObject(processInfo.hProcess, 20) == WAIT_OBJECT_0)
break;
PROCESS_INFORMATION processInfo;
STARTUPINFOA si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
memset(&processInfo, 0, sizeof(processInfo));
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
HANDLE stdOut;
CreatePipe(stdOut, si.hStdOutput, false);
HANDLE stdErr;
CreatePipe(stdErr, si.hStdError, false);
si.dwFlags = STARTF_USESTDHANDLES;
DWORD startTick = GetTickCount();
BOOL worked = CreateProcessA(NULL, (char*)cmdLine.c_str(), NULL, NULL, TRUE,
flags, envPtr, NULL, &si, &processInfo);
::CloseHandle(si.hStdOutput);
::CloseHandle(si.hStdError);
if (!worked)
return 1;
DWORD threadId;
ProcParams stdOutParams = { stdOut, GetStdHandle(STD_OUTPUT_HANDLE) };
HANDLE stdOutThread = ::CreateThread(NULL, (SIZE_T)128 * 1024, (LPTHREAD_START_ROUTINE)ReadProc, (void*)&stdOutParams, 0, &threadId);
ProcParams stdErrParams = { stdErr, GetStdHandle(STD_ERROR_HANDLE) };
HANDLE stdErrThread = ::CreateThread(NULL, (SIZE_T)128 * 1024, (LPTHREAD_START_ROUTINE)ReadProc, (void*)&stdErrParams, 0, &threadId);
while (true)
{
if (::WaitForSingleObject(processInfo.hProcess, 20) == WAIT_OBJECT_0)
break;
}
::WaitForSingleObject(stdOutThread, INFINITE);
::WaitForSingleObject(stdErrThread, INFINITE);
DWORD exitCode = 0;
::GetExitCodeProcess(processInfo.hProcess, &exitCode);
printf("Exit code: %d\n", exitCode);
if ((exitCode == 0) || (pass == 1))
return exitCode;
printf("FAILED! Starting second attempt.\n");
}
::WaitForSingleObject(stdOutThread, INFINITE);
::WaitForSingleObject(stdErrThread, INFINITE);
DWORD exitCode = 0;
::GetExitCodeProcess(processInfo.hProcess, &exitCode);
return exitCode;
return 0;
}

View file

@ -23,32 +23,32 @@
<ProjectGuid>{BE0C7160-375C-4164-8388-BFCC6DAA7828}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>RunAndWait</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

Binary file not shown.