Is Execute Thread Safe Code?

Based on this article :

I use this code to do some animation for a given Window Handle, doing some work in my database:

while not Terminated do 
begin
// some code....

// draw onto the Window DC
DC := GetDC(FWnd); // FWnd is the Window Handle
// DC := GetDCEx(FWnd, 0, DCX_VALIDATE or DCX_LOCKWINDOWUPDATE);
if DC <> 0 then
  try
    BitBlt(DC,
      FPaintRect.Left,
      FPaintRect.Top,
      ImageRect.Right,
      ImageRect.Bottom,
      Bitmap.Canvas.handle,
      0, 0,
      SRCCOPY);
  finally
    ReleaseDC(FWnd, DC);
  end;

  // more code....

end; // end while

Is it thread safe, or should I somehow block the DC?

Also, can I use GetDCEx? Thank.

0
source share
1 answer

No, your code is not thread safe if a window handle ( FWnd) is created in the main (GUI) thread . The standard VCL approach is to call all the GDI functions in a GUI thread using methods Synchronizeor a Queueclass TThread.

+2
source

All Articles