Show: Delphi C++
Display Preferences

System.Classes.TThread.Synchronize

From XE2 API Documentation
Jump to: navigation, search

Delphi

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

C++

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

Contents

Properties

Type Visibility Source Unit Parent
procedure
function
protected
System.Classes.pas
System.Classes.hpp
System.Classes TThread

Description

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 current thread is passed in the AThread parameter.

If you are unsure whether a method call is thread-safe, call it from within the Synchronize method to ensure 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.
Note: When using the static version of the Synchronize method with nil/NULL as a first parameter, it will synchronize the method referenced by AMethod within the main thread.

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

Synchronize(
  procedure
  begin
    Form1.Memo1.Lines.Add(Begin Execution’);
  end);

See Also

Code Examples

Personal tools
Previous Versions
Translations