Sending a Message Using the Windows Message Queue

From RAD Studio
Jump to: navigation, search

Go Up to Sending Messages


In a multithreaded application, you can not just call the Perform method because the target control is in a different thread than the one that is executing. However, by using the Windows message queue, you can safely communicate with other threads. Message handling always occurs in the main VCL thread, but you can send a message using the Windows message queue from any thread in the application. A call to SendMessage is synchronous. That is, SendMessage does not return until the target control has handled the message, even if it is in another thread.

Use the Windows API call, SendMessage, to send a message to a control using the Windows message queue. SendMessage takes the same parameters as the Perform method, except that you must identify the target control by passing its Window handle. Thus, instead of writing

MsgResult := TargetControl.Perform(MY_MYMESSAGE, 0, 0);
MsgResult = TargetControl->Perform(MY_MYMESSAGE, 0, 0);

you would write

MsgResult := SendMessage(TargetControl.Handle, MYMESSAGE, 0, 0);
MsgResult = SendMessage(TargetControl->Handle, MYMESSAGE, 0, 0);

For more information on the SendMessage function, see the Microsoft MSDN documentation. For more information on writing multiple threads that may be executing simultaneously, see Coordinating Threads.

See Also