我正在使用CreateAnonymousThread作为工作任务,当我开始使用它时,我在整个声明中使用了Synchronize,根据文档示例,例如:
procedure Txxx.RunWorker;
begin
FExecutionThread := TThread.CreateAnonymousThread(procedure ()
begin
TThread.Synchronize (TThread.CurrentThread,
procedure ()
begin
// Here before worker stuff
NotifyBeforeWorkerStuff;
end);
// Do worker stuff
TThread.Synchronize (TThread.CurrentThread,
procedure ()
begin
// Here after worker stuff
NotifyAfterWorkerStuff;
end);
end);
FExecutionThread.Start;
end;
end;
Run Code Online (Sandbox Code Playgroud)
如您所见,在此线程中,我向我的应用程序的各个部分启动事件通知,包括VCL表单(NotifyBeforeWorkerStuff等).后来,我看到我可以将Synchronize()更多地本地移动到每个VCL表单,接近实际需要它更新(非安全)VCL控件的点:
procedure TSomeVCLForm.ReceiveNotification;
begin
TThread.Synchronize (TThread.CurrentThread,
procedure ()
begin
Label1.Caption := GetSomeStringFunction;
end);
end;
Run Code Online (Sandbox Code Playgroud)
只要我接收来自主线程或工作线程的通知,工作线程就会变得更简单:
procedure Txxx.RunWorker;
begin
FExecutionThread := TThread.CreateAnonymousThread(procedure ()
begin
NotifyBeforeWorkerStuff;
// Do worker stuff
NotifyAfterWorkerStuff;
end);
FExecutionThread.Start;
end;
Run Code Online (Sandbox Code Playgroud)
关于这是否正确,我有几个问题:
谢谢你的建议.