(Please provide a description in your own words. It is illegal to use the wording from the Delphi Help.)
(Known issues / Documentation clarifications / Things to be aware of)
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.)
(Please provide links to articles/source code that show how to use this item.)
(Please provide links to items specifically related to this item.)
(Please leave your name with your comment.)