Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Watching for a process to start

Name: Anonymous 2008-07-09 12:25

Sup /prog/,

Trying to find an elegant way to watch for when a specific process/application starts in C++. Could of course do something like EnumProcesses() from the PSAPI and go through the list to see if the one I'm looking for is in it... and put that on a timer to poll for it every so often.

But that seems rather messy and polling is evil. Not to mention the process will show up there before it's fully loaded into memory if I'm not mistaken... And I need it to be completely loaded and running.

Anyone know of a better/cleaner way to do it?

Name: Anonymous 2008-07-09 15:30

You must either register a callback or use a polling approach. Both have been discussed, are you holding out for some magical third option?

Ooh, I know, you can use quantum polling! Choose any extent of time as an acceptable response delay. Now pick a random point of time in the future and sleep until then. Wake up and check if the process is running. If it is not, destroy the universe.
If the process is running, ask for its running time. If the process have been running for longer than your acceptable response delay, destroy the universe.
This guarantees that you in the remaining universes will have observed the thread sleep until the process starts, and then wake up and poll once[1][2].
___________
1. This assumes that the process you're monitoring will only be run once. Refining the algorithm to notice if the detected event is not the first execution of the program and modify its behavior accordingly is left as an exercise to the reader.
2. You will also observe your program oversleeping and ignoring the process in a large amount of universes. Don't worry, this will eventually rectify itself. The algorithm guarantees correct behavior in the end.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List