Multithreading – cross thread communication in Delphi
Is there a document about cross thread communication in Delphi? How do I send messages to threads without windows?
Solution
You can only send (Windows) messages to threads that implement the standard message loop, which will be generated automatically after the window handle is implemented
However, it is not necessary to use messages to communicate with threads Let it wait for the event object (tevent in VCL) and signal this event when you want the thread to execute the function
However, if you are not familiar with multithreading – don't study all these details yourself unless you want to learn the effects Just use omnithreadlibrary Once you know how to use it, you can learn a lot by mining its internal structure
Edit:
The answer to this question is very similar
Edit 2:
About the comment "what is easier for [omnithreadlibrary] and what is the cost?" I can only suggest you check it yourself - that is, you should at least use Delphi 2007 There are several examples to illustrate these concepts, but for a quick "real-life" example, you can take a look at this blog post - you don't even need to install the library for this
I also agree that multithreading with libraries does require some belief behavior The function of Otoh using VCL is almost irreplaceable The sample code still uses the wrong synchronize () call Producer - consumer queues that do not support thread safety are more suitable for multithreaded programming If you agree that you need a more solid foundation for multithreading than VCL - why reinvent that particular wheel?
As for the cost of using the Library: you must check whether the time is fast enough It does abstract the communication between threads in a good way, with all due respect, but obviously every abstraction degrades performance
If you think it's not for you after all – write your own code I did the same thing with Delphi 4. I have used the code for nearly 10 years Judging from the number of errors I found and the corner situations I encountered during that time, I would certainly advise anyone new to multithreading not to write their own library code for it If you really want to, please keep the rules in this posting in mind