Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need synchronize in TThread?

I know that you need synchronize (yourprocedure) to set e.g. a label's text. But what about:

  1. Reading a label's text.
  2. Toggle/Set the label's enabled property.
  3. Call other labels procedures/functions (e.g. onclick event).

Is there an easy rule to know/remember when I need to use synchronize?

PS.: Is synchronize similar to PostMessage/SendMessage?

like image 428
Ben Avatar asked Dec 08 '22 15:12

Ben


2 Answers

Easy rule of thumb: ANY access to VCL UI components needs to be synchronized. That includes both reading and writing of UI control properties. Win32 UIs, most notably dialogs like MessageBox() and TaskDialog(), can be used directly in worker threads without synchronizing.

TThread.Synchronize() is similar to SendMessage() (in fact, it used to be implemented using SendMessage() internally in Delphi 5 and earlier). TThread.Queue() is similar to PostMessage().

like image 94
Remy Lebeau Avatar answered Dec 31 '22 15:12

Remy Lebeau


Any time you access a VCL UI component, you need to implement some type of thread safety measure. This is also, typically, the case when you're accessing a variable or procedure that exists or will be accessed by another thread. However, you don't need to use the Synchronize method in all of these situations. There are other tools at your disposal, and Synchronize is not always your best solution.

Synchronize blocks both the main thread and the calling thread while it's performing the procedure that you pass to it, so overusing it can detract from the benefits of multi-threading. Synchronize is probably most commonly used for updating your UI, but if you find that you're having to use it really frequently, then it might not be a bad idea to check and see if you can restructure your code. I.E. do you really need to read labels from within your thread? Can you read the label before starting the thread and pass it into the thread's constructor? Can you handle any of these tasks in the thread's OnTerminate event handler?

like image 28
Aaron Avatar answered Dec 31 '22 14:12

Aaron