I have a thread in some console process, whose code is not available to me. Is there a way to get notified when its state changes (e.g. becomes idle)?
Maybe hooks API or even kernel-mode API (if no other way...)?
Expansion:
I have legacy console app I have to operate in my program. The only way to interact with it, obviously, is via stdin
. So I run it as new Process
, and send commands to its stdin
. I take some text from its stdout
, but it is not entirely predictable, so I cannot rely on it in order to know when it finished its current work and ready to receive the next commands. The flow is something like this: I run it, send (e.g.) command1
, wait for it to finish its work (some CPU
load and some IO
operations), and then issue command2
, and so on, until last command. When it finished working on last command, I can close it gracefully (send exit
command). The thing is that I have several processes of this console exe that are working simultaneously.
I can:
- use timeouts since last
stdout
received - not good, because it can be a millisecond and an hour - parse
stdout
using (e.g.)regex
to wait for expected outputs - not good... the output is wholly unexpected. It's almost random - using timers, poll its threads state and only when all of them are in
wait
state (and not forIO
), and at least one is waiting for user input (see this) - not good either: in case I use many processes simultaneously it can create unnecessary, non-proportional burden on the system.
So I like the last option, just instead polling, I rather events fired when these threads become idle.
I hope it explains the issue well...