Delphi Thread线程错误:Canvas doesn't allow drawing
错误原因:在使用Thread线程时,所有UI界面的代码都要进行同步,否则,就会出现这个错误。
解决方法:
TThread.Synchronize();
Synchronize 源码:
procedure TThread.Synchronize(Method: TThreadMethod);
begin
FSynchronize.FThread := Self;
FSynchronize.FSynchronizeException := nil;
FSynchronize.FMethod := Method;
Synchronize(@FSynchronize);
end;
class procedure TThread.Synchronize(AThread: TThread; AMethod: TThreadMethod);
var
SyncRec: TSynchronizeRecord;
begin
if AThread <> nil then
AThread.Synchronize(AMethod)
else
begin
SyncRec.FThread := nil;
SyncRec.FSynchronizeException := nil;
SyncRec.FMethod := AMethod;
TThread.Synchronize(@SyncRec);
end;
end;
class procedure TThread.StaticSynchronize(AThread: TThread; AMethod: TThreadMethod);
begin
Synchronize(AThread, AMethod);
end;
创建时间:2020.10.13 更新时间: