Wikia

Delphi Programming

TThread Class

2,918pages on
this wiki
Talk0
This page is intended as a supplement to the official documentation on Delphi programming. CodeGear is in the process of putting the Delphi documentation on the Web. Once they have done so, this page will link to the relevant page in the official documentation.
Stub
This article is a stub.
Please help enhance the Delphi Programming Wiki by expanding it.
Info
see the VCL Documentation Guidelines for an overview on doc pages

UnitEdit

HierarchyEdit

DescriptionEdit

(Please provide a description in your own words. It is illegal to use the wording from the Delphi Help.)

Technical CommentsEdit

(Known issues / Documentation clarifications / Things to be aware of)

Synchronize methodEdit

The TThread.Synchronize method is available in three overloaded versions (Delphi 2007):

 private
   class procedure Synchronize(ASyncRec: PSynchronizeRecord; QueueEvent: Boolean = False); overload;
 protected
   procedure Synchronize(AMethod: TThreadMethod); overload;
 public
   class procedure Synchronize(AThread: TThread; AMethod: TThreadMethod); overload;

  • The first version is private and therefore only used internally.
  • The second version is meant to be called from within the thread object itself.
  • The third is a class method to be called from outside and therefore needs the thread object as the first parameter. This parameter can be nil, though.

There is also a StaticSynchronize method, that just calls the third version and has the same parameters.

The method is used to execute a method in the context of the application's main thread, e.g. to access the (non-threadsafe) VCL. For this it uses a rather complicated approach that on the other hand seems to be faster and more reliable than most other solutions I have seen (in particular it is faster than using PostMessage). The implementation of TThread.Synchronize has changed several times over the years.

To avoid a deadlock, you must make sure that your main thread is able to execute its message loop when Synchronize is being called. If that's not possible, call the procedure CheckSynchronize like this:

 // Called in the main thread, it is assumed that the
 // Thread sets SomeThread to nil when it terminates.
 SomeThread.Terminate;
 while Assigned(SomeThread) do
   CheckSynchronize;

(Alternatively you can call SomeThread.WaitFor, the above is just an example.)

ExamplesEdit

(Please provide links to articles/source code that show how to use this item.)

See AlsoEdit

(Please provide links to items specifically related to this item.)

User Comments/TipsEdit

(Please leave your name with your comment.)

Around Wikia's network

Random Wiki