Click to See Complete Forum and Search --> : WaitForSingleObject not waiting for anybody


majesticdawn
March 8th, 2008, 08:32 PM
I have been trying for HOURS to get my program to launch an external program and then wait until it closes before it launches the next external program. Here's my code. Please point out my error if you can find it. Thanks for trying.



void exeCommand(char* command){
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
command, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)

// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);

// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

DWORD WINAPI StartThread1(LPVOID iValue){
exeCommand("notepad.exe \"c:\\test.txt\"");
return(0);
}

DWORD WINAPI StartThread2(LPVOID iValue){
exeCommand("calc.exe");
return(0);
}




HANDLE hThread1;
DWORD dwGenericThread;
char lszThreadParam[3];

strcpy(lszThreadParam,"3");
hThread1 = CreateThread(NULL,0,StartThread1,&lszThreadParam,0,&dwGenericThread);
if(hThread1 == NULL){
DWORD dwError = GetLastError();
//yeah, do something... or just set a breakpoint
}
WaitForSingleObject(hThread1,20000);

hThread1 = CreateThread(NULL,0,StartThread2,&lszThreadParam,0,&dwGenericThread);
if(hThread1 == NULL){
DWORD dwError = GetLastError();
//yeah, do something... or just set a breakpoint
}
WaitForSingleObject(hThread1,INFINITE);

MikeAThon
March 9th, 2008, 07:20 PM
So what's not working? Your code has four different WFSO's in it, so we can't tell which one is the one you are complaining about.

Put some breakpoints in the code, and tell us the behavior you are seeing vs. the behavior you expect.

Mike

Igor Vartanov
March 16th, 2008, 06:28 AM
// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
command, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)

// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);Look what you're doing: you try to wait if CreateProcess fails. :D Remove that negation, and you're fine. :wave:

PS. Apparently you've missed the failure handler block. ;)