|
First off, I'm still new to coding in C++ and so I hope this is the right forum for my question...
I'm coding a very small utility for a niche need we've got where I work. Right now I've got things coded so that it uses CreateProcess to fire off another program we run. What I need to do is then have this utility stay open and running (the program that it kicks off can terminate the original program once it's done doing its thing - and I can't just wait for a return code because the program that gets kicked off may kick off others, and one of those might be the final item to run).
Right now it launches our program and then, since it's done doing all the code, it finishes up and quits. Is there any way to tell the thing to stay open and running? Kind of like a service (but not a service because I don't want to install anything).
|
|
|
|
|
Just add a loop with a timer, and whatever other functions you need. The sequence would then be something like:
while TRUE
if applicationFlag is TRUE
then
start external application
set applicationFlag FALSE
end if
sleep for some seconds
end while
You will need to add some other decision code to set the applicationFlag to TRUE at some point.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
What if the program remains open for an hour or three (I hope they close it sooner, but I have to think of the "what ifs")? Is the Sleep command going to be the right way to go, or am I going to create a problem when using it over an extended period of time?
|
|
|
|
|
The other alternative is to use a timer: Timers Tutorial[^] is a great article which shows how to implement a few.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
If I'm understanding correctly (I'm not certain I am, your description is anything but clear) something like this might help (you can wait on a process handle):
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bOk = CreateProcess(
_T("C:\\Windows\\system32\\notepad.exe"),
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi
);
if (bOk)
{
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
MessageBox(NULL, _T("Finished"), _T("Create and wait"), MB_OK);
}
Steve
|
|
|
|