How to send a message to a TThread from main thread in Delphi?

前端 未结 3 1457
余生分开走
余生分开走 2021-02-04 16:17

I want to send a message to a thread and handle it in the thread. How can I do this in Delphi? I guess PostMessage is the way to go, but the examples I\'ve seen so

3条回答
  •  萌比男神i
    2021-02-04 17:02

    You can either have a message loop (possibly with a hidden notification window) in your thread and send a Windows message to it, or you can use a more native (less-GUI) way of doing it, such as a queue protected by a critical section combined with a manual-reset event that the thread waits on and the sending thread signals.

    A more general solution is a producer-consumer queue, which in the classic implementation uses a couple of semaphores to keep track of consumers and producers and a third semaphore for mutually exclusive access to the queue; however, more optimal producer-consumer queues are available on the net.

提交回复
热议问题