System.Classes.TThread.Synchronize
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.
An example of when you can 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.
Delphi:
Synchronize(
procedure
begin
Form1.Memo1.Lines.Add('Begin Execution');
end);
C++:
String timeStarted = DateTimeToStr(Now());
TThread::CreateAnonymousThread(
[timeStarted]() {
LengthyOperation(); // Some lengthy work done in worker thread
TThread::Synchronize(nullptr, _di_TThreadProcedure([timeStarted] {
ShowMessage(String("DONE: Job started @ ")+timeStarted);
}));
})->Start();
See Also
- System.Classes.TThreadMethod
- System.Classes.TThread.Execute
- System.Classes.TThread.Queue
- System.Classes.TThread.OnTerminate
- System.SyncObjs.TCriticalSection
- System.SysUtils.TMultiReadExclusiveWriteSynchronizer
- System.Classes.CheckSynchronize
- Using the Main VCL Thread