domehead100 replied to Igor Tandetnik
05-Nov-09 07:40 PM

ith PostMessage, then it already has race
essage, your application will crash and
I think there is an inevitable race condition. I am trying to get in
and manipulate the target app while the user is potentially typing
keystrokes. When they type a key that requires my app to take action,
which may involve inserting keystrokes into the input queue, it needs
to begin manipulating the target app before the user presses the next
key or at least before the target app processes that next keystroke
message. I am trying to always win the race.
indows features preemptive multitasking,
simultaneously.
I have no control over a context switch; however, as soon as the DLL
calls SendMessageTimeout, the target app is blocked until I return or
reply to it (or the timeout occurs). So, the target will not get a
time slice or if it does it will not do anything because it is
blocking. With PostMessage, as soon as the DLL calls PostMessage, the
target app is free to do whatever immediately. The difference is that
with SendMessage, I can take a few milliseconds to: a) determine if I
need to take action, and b) do some additional processing if needed,
before returning or replying to the message. By the time ReplyMessage
is called, my app has already determined what it needs to do (often
just SendInput), has queued up whatever data it needs, and is ready
virtually in the next line of code after ReplyMessage to take action
(e.g., SendInput). The race is between the time I ReplyMessage and
the time I take action. With PostMessage, the race begins as soon as
PostMessage is called. I am trying to get a head start to make sure I
always win the race, if that makes sense. This has all worked
superbly, only now I am trying to add functionality of scripting or
manipulating the target app through automation, and now I have run into
this COM safety feature. I am not complaining about it, it makes
sense, I was just confused by the documentation.
Thanks again for taking the time to help me with this. I realize that
perhaps I am being stupid or stubborn or whatever; hope I have not
been irritating though :o).
I will try PostMessage and see how that works, and probably some
combination of events as well (I do not think events will be all that
complex if I do not have to add another thread).
~Mike