From RAD Studio API Documentation
Jump to: navigation, search


procedure Synchronize(AMethod: TThreadMethod); overload; inline;
procedure Synchronize(AThreadProc: TThreadProcedure); overload; inline;
class procedure Synchronize(const AThread: TThread; AMethod: TThreadMethod); overload; static;
class procedure Synchronize(const AThread: TThread; AThreadProc: TThreadProcedure); overload; static;


void __fastcall Synchronize(TThreadMethod AMethod)/* overload */;
void __fastcall Synchronize(_di_TThreadProcedure AThreadProc)/* overload */;
static void __fastcall Synchronize(TThread* const AThread, TThreadMethod AMethod)/* overload */;
static void __fastcall Synchronize(TThread* const AThread, _di_TThreadProcedure AThreadProc)/* overload */;


Type Visibility Source Unit Parent
System.Classes TThread


Executes a method call within the main thread.

Synchronize causes the call specified by AMethod to be executed using the main thread, thereby avoiding multithread conflicts. The AThread parameter associates the caller thread.

For static methods, you can associate AMethod with any thread using the AThread parameter. Also, you can use nil/NULL as AThread parameter if you do not need to know the information for the caller thread in the main thread.

In the current implementation, the Synchronize method can use associated thread information to wake-up the main thread on Windows platforms.

If you are unsure whether a method call is thread-safe, call it from within the Synchronize method to ensure that it executes in the main thread.

Execution of the current thread is suspended while the method executes in the main thread.

Warning: Do not call Synchronize from within the main thread. This can cause an infinite loop.
Note: You can also protect unsafe methods using critical sections or the multiread exclusive-write synchronizer.

An example of when you would want to use Synchronize is when you want to interact with either a VCL or a FireMonkey component. Use an in-place anonymous method to solve the problem of passing variables to the method you want to sychronize:

    Form1.Memo1.Lines.Add(Begin Execution);

See Also

Code Examples