title: How is executed a SendMessage from a different thread?
body: <p>When we send a message, "<em>if the specified window was created by the calling thread, the window procedure is called immediately as a subroutine</em>".
  But "<em>if the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code.</em>" (taken from MSDN documentation for <code>SendMessage</code>).</p>
  
  <p>Now, I don't understand <em>how</em> (or, more appropriately, <em>when</em>) the target windows procedure is called. Of course the target thread will not be preempted (the program counter is not changed). I presume that the call will happen during some wait function (like <code>GetMessage</code> or <code>PeekMessage</code>), it is true? That process is documented in detail somewhere?</p>
  
  <hr>
  
  <p><strong>Update:</strong> the rationale behind it is explained by the <code>QS_SENDMESSAGE</code> flag of <code>GetQueueStatus()</code> and <code>MsgWaitForMultipleObjects()</code>:</p>
  
  <pre><code>QS_SENDMESSAGE
   A message sent by another thread or application is in the queue.
  </code></pre>
  
  <p>This, along with additional remarks in MSDN documentation, means that a message sent by another thread is actually posted to the queue. Then, as soon as <code>GetMessage</code> or <code>PeekMessage</code> are called, it will be processed before any other posted message by being  sent directly to the window procedure.</p>