RAD Studio |  |

Sending a Message Using the Windows Message Queue
See Also
Collapse All

In a multithreaded application, you can't 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

Copy Code

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

Copy Code

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

you would write

Copy Code

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

Copy Code

    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

Sending a Message That Does Not Execute Immediately

Broadcasting a Message to All Controls in a Form

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!