C++ Builder 参考手册 ➙ System::Classes ➙ TThread ➙ Synchronize
头文件:#include <System.Classes.hpp>
命名空间:System::Classes
类:TThread
访问权限:public: / protected:
函数原型:
public:
static void __fastcall Synchronize(TThread* const AThread, TThreadMethod AMethod);
static void __fastcall Synchronize(TThread* const AThread, _di_TThreadProcedure AThreadProc);
protected:
void __fastcall Synchronize(TThreadMethod AMethod);
void __fastcall Synchronize(_di_TThreadProcedure AThreadProc);
Synchronize 是 System::Classes::TThread 的成员函数,在主线程里面同步执行一个函数。
参数:
AThread:要进行异步执行的函数所在的线程对象,如果不需要在主线程调用那里访问线程对象,这个参数可以为 NULL;
AMethod:需要在主线程异步执行的方法;
AThreadProc:需要在主线程异步执行的 lambda 表达式或匿名函数。
返回值:
无。
- Synchronize 方法在主线程里面执行一个函数,并且等待这个函数执行结束;
- 如果要在主线程里面执行一个函数,不等待这个函数执行结束,需要用 Queue 方法。
例1:在主线程同步执行线程类的 AMethod 方法
class TTestThread : public System::Classes::TThread
{
private:
void __fastcall SetLabelValue(void)
{
Form1->Label1->Caption = StrToIntDef(Form1->Label1->Caption,0) + 1;
}
protected:
void __fastcall Execute(void)
{
while(!Terminated)
{
Synchronize(SetLabelValue);
Sleep(200);
}
}
};
TTestThread *lpTestThread = NULL;
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(!lpTestThread)
{
lpTestThread = new TTestThread;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(lpTestThread)
{
lpTestThread->Terminate();
lpTestThread->WaitFor();
delete lpTestThread;
lpTestThread = NULL;
}
}
例2:在主线程同步执行 lambda 表达式
class TTestThread : public System::Classes::TThread
{
protected:
void __fastcall Execute(void)
{
while(!Terminated)
{
Synchronize([this](){
Form1->Label1->Caption = StrToIntDef(Form1->Label1->Caption, 0) + 1;
});
Sleep(200);
}
}
};
TTestThread *lpTestThread = NULL;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(!lpTestThread)
{
lpTestThread = new TTestThread;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(lpTestThread)
{
lpTestThread->Terminate();
lpTestThread->WaitFor();
delete lpTestThread;
lpTestThread = NULL;
}
}
例3:在主线程同步执行 AThreadProc 匿名函数不使用 lambda 表达式
class TSetLabelValue : public System::TCppInterfacedObject<TThreadProcedure>
{
public:
void __fastcall Invoke(void)
{
Form1->Label1->Caption = StrToIntDef(Form1->Label1->Caption, 0) + 1;
}
};
class TTestThread : public System::Classes::TThread
{
protected:
void __fastcall Execute(void)
{
while(!Terminated)
{
Synchronize(new TSetLabelValue);
Sleep(200);
}
}
};
TTestThread *lpTestThread = NULL;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(!lpTestThread)
{
lpTestThread = new TTestThread;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(lpTestThread)
{
lpTestThread->Terminate();
lpTestThread->WaitFor();
delete lpTestThread;
lpTestThread = NULL;
}
}
例4:在匿名线程里面调用 Synchronize 静态方法的例子
请参考 CreateAnonymousThread 方法的例子。
相关:
- System::TCppInterfacedObject
- System::Classes::TThreadProcedure
- System::Classes::_di_TThreadProcedure
- System::Classes::TThread::Queue
- System::Classes::TThread::ForceQueue
- System::Classes::TThread::RemoveQueuedEvents
- System::Classes::TThread::CreateAnonymousThread
- System::Classes::TThread
- System::Classes::TComponent::BeginInvoke
- System::Classes::TComponent::EndInvoke
- System::Classes::TComponent::EndFunctionInvoke
- System::TObject
- VCL 类继承关系
C++ Builder 参考手册 ➙ System::Classes ➙ TThread ➙ Synchronize