PropertyValue
rdfs:label
  • TThread Class
rdfs:comment
  • The TThread.Synchronize method is available in three overloaded versions (Delphi 2007): <pre> 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;</pre> * 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.
dcterms:subject
dbkwik:delphi/property/wikiPageUsesTemplate
abstract
  • The TThread.Synchronize method is available in three overloaded versions (Delphi 2007): <pre> 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;</pre> * 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: <pre> // 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;</pre> (Alternatively you can call SomeThread.WaitFor, the above is just an example.)