I am using the ShellExecute command to run an executable file several times using a while loop. However, many times the executable isn't finished running before the next one is called. How can I keep this from happening?
You can write your own ShellExecute with CreateProcess and synchronize the call with WaitForSingleObjectEx on the process handle. The attached sample procedure uses this technique:
[tt]
BOOL SpawnProc(int CreationFlags, BOOL Sync, int argc, char *argv[])
{
// va_list argptr;
// char *parm = Param, CmdLine[256];
char CmdLine[256];
int i = 0;
PROCESS_INFORMATION ProcessInformation;
// LPSECURITY_ATTRIBUTES lpProcessAttributes, lpThreadAttributes;
STARTUPINFO si;
// LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_STARTING_SERVICE, "CreateProcess ", CmdLine);
if (CreateProcess(
argv[0], // pointer to name of executable module
CmdLine, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
CreationFlags, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&si, // pointer to STARTUPINFO
&ProcessInformation // pointer to PROCESS_INFORMATION
))
{
if (Sync) {
::WaitForSingleObjectEx(
ProcessInformation.hProcess, // handle of object to wait for
INFINITE, // time-out interval in milliseconds
FALSE // return to execute I/O completion routine if TRUE
);
}
return TRUE;
} else {
// LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_PROC_START_FAIL, CmdLine);
return FALSE;
};
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.