Show: Delphi C++
Display Preferences

System.Classes.TThread.Synchronize

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

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;

C++

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 */;

Properties

Type Visibility Source Unit Parent
procedure
function
public
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 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:

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

See Also

Code Examples

Personal tools
RAD Studio 10.2 Tokyo
In Other Languages
Previous Versions
Assistance