Seems like you are not completely clear how windows messages are handled in
Windows (Win32) and as such Windows.Forms.
Consider the example you posted recently:
In this you have:
- a main thread handling the UI and,
- two worker threads (called workers), handling external communication and
dispatching messages (Text) to the UI thread.
Because the workers may not directly touch the UI thread affinitized window
handles, you correctly opted to use Control.Invoke to marshal the delegate
to the UI thread.
What basically happens when you call Invoke is :
- Put a Thread Method Entry object in a Linked list ( a queue), this TME
contains things like caller, target HWND, method delegate, arguments, the
compressed caller stack etc.
- Post a private USER Windows message (calling Win32 PostMessage) to the UI
thread application message queue.
- Wait for an Event that signals the end of the delegate method. (This is
what differentiates BeginInvoke from Invoke, in that BeginInvoke doesn't
wait but returns immediately)
When the UI thread message loop retrieves the private USER message from it's
application "message queue" in it's message handler, it also pulls the TME
from the linked list and executes the TME's method(function, eventhandler
etc..) on it's own thread (UI) using the arguments supplied and the
compressed caller stack. Whenever the method returns, the message handler
set's an Event signaling the caller that a method ran to completion. and
continues handling next application message.
If there's no further message in the UI thread's application message queue
(no further Posted messages), the Windows system will inspect the "input
message" queue and switch the message loop to this queue if there are input
messages (mouse, keyboard), however, as soon as there are new messages in
the application queue Windows will switch back to this queue for its message
processing. [note that not all input messages are placed in the input
queue!!]
What this all means is that Windows queues are prioritized by windows, that
means that posted messages have higher priority than input messages, and as
long as there are posted messages, no input messages (mouse clicks f.i) are
getting process. Worse, input messages are processed one at a time, that
means that a key-down event can be handled before a bunch of posted messages
followed by the handling of the key-up event.
To you it means that you should allow the message queue to drain at regular
intervals in order to allow input messages to be processed.
Now I still wonder why someone is placing all this stuff on the screen,
while I see hardly someone reading this at this speed.
Willy.
Brian Keating EI9FXB said:
Would I be correct in saying that the only way to get a user message into
a
Windows form would be to use P/Invoke with [Send/Post]Message?
Of is there some part of the .NET API that I am totally un aware of?
Thanks for any help
Brian