我的执行代码线程安全吗? [英] Is my Execute code Thread Safe?

查看:66
本文介绍了我的执行代码线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此文章:

我使用此代码在给定的窗口句柄上制作一些动画,同时在数据库上完成一些工作:

I use this code to make some animation on a given Window Handle while doing some job on 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

它是线程安全的,还是应该以某种方式锁定DC?

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

此外,我可以使用GetDCEx吗? 谢谢.

Also, Can I use the GetDCEx? Thanks.

推荐答案

否,假设窗口句柄(FWnd)是在主(GUI)线程中创建的,则您的代码不是线程安全的.一种标准的VCL方法是通过TThread类的SynchronizeQueue方法调用GUI线程中的所有GDI函数.

No, your code is not threadsafe assuming that the window handle (FWnd) is created in main (GUI) thread. A standard VCL approach is to call all GDI functions in GUI thread, via Synchronize or Queue methods of TThread class.

这篇关于我的执行代码线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆