Message queueing with threads in Delphi

179 views Asked by At

I'm new at message queueing, and want to implement something like this:

  • Get information from main thread

  • Send to a worker thread queue

  • Worker thread may just process it or send to another work thread proccess it

  • Eventually callback execution to UI due some events (can handle it with synchronize mechanism)

My main problem is how to make communication between main thread and worker thread's, and between worker threads with other worker threads (to signal the main thread eventually, I can callback then synchronized).

After tons of googling, I found basically two ways to do it:

  • Create a message-only HWND, which exchange messages between a registered WndProc() function. I found a perfect example of that here. As Remy Lebeau commented, this way may need to dispatch the message via DispatchMessage() (and seems to be more verbose way to do it). This way basically I use the PostMessage() function and process messages in the WndProc() function.

Or:

  • Create a custom TThread-derived class with a PeekMessage()|GetMessage() loop which processes thread messages and deals with what it needs to do in its Execute() method. This one basically I use the PostThreadMessage() function and process messages in the Execute() method of the thread;

I successfully tested a basic demo with the two options, receiving the messages in the two ways I mentioned.

For those who have more expertise, am I on the right track? Did I miss something trying to understand how to do this?

Another question for those more experienced is, which method is the best for heavy message exchanging? And which is the best for an X situation or Y situation?

It's a more theoretical question, but thanks for those who can help.

0

There are 0 answers