它是同步的问题。 我做了一个简化我的问题,所以我有一个VCL定时器和线程数。 该线程试图写的东西在两个位图,以及定时器试图将位图绘制成图像(TImage中)。 这里是我的代码修改更容易理解(本贴的代码是非常模糊)。
//------------------------------------------------------------------------------
Procedure TMyForm.Add(iX,iY,iNr:integer);
begin
EnterCriticalSection(csCriticalSection);
bmRed.Canvas.Lock;
bmBlue.Canvas.Lock;
//.... drawing etc...
bmRed.Canvas.TextOut(iX,iY,IntToStr(iNr));
bmBlue.Canvas.TextOut(iX,iY,IntToSTr(iNr));
bmRed.Canvas.Unlock;
bmBlue.Canvas.Unlock;
LeaveCriticalSection(csCriticalSection);
end;
//------------------------------------------------------------------------------
procedure TMyForm.tmTimer(...);
begin
EnterCriticalSection(csCriticalSection);
bmRed.Canvas.Lock;
bmBlue.Canvas.Lock;
//Drawing on bmRed and bmBlue
imBlue.Canvas.Draw(bmBlue);
imRed.Canvas.Draw(bmBlue);
bmRed.Canvas.Unlock;
bmBlue.Canvas.Unlock;
LeaveCriticalSection(csCriticalSection);
end;
//------------------------------------------------------------------------------
Procedure TMyThread.Execute();
begin
Randomize;
while not terminated do
begin
MyFormInstance.Add(Random(100),Random(100),Random(100));
Sleep(20);
end;
end;
initialization
InitializeCriticalSection(csCriticalSection);
finalization
DeleteCriticalSection(csCriticalSection);
通常情况下,线程临界区或几秒钟后,定时器进入将不再吸引我什么时候给我的错误。