Getting a Delphi TTimer to work with a multi-threading app

回眸只為那壹抹淺笑 提交于 2019-12-05 21:05:37

TTimer is a message based timer. Whatever thread context the TTimer is created in must have an active message loop in order for TTimer to process its WM_TIMER messages.

TTimer is not a thread-safe timer. In order to receive the WM_TIMER messages, it has to allocate an HWND window handle for itself. It does so using the VCL's AllocateHWnd() function, which is not thread-safe and must not be called from outside the context of the main thread.

If you need a thread-safe timer, either call CreateWindow() directly and pump/process the WM_TIMER messages directly, or else use a different timer mechanism, such as a threaded multimedia timer via timeSetEvent(), or even just a simple busy loop via Sleep() or WaitForSingleObject(). Without knowing what you are using the timer for, it is difficult to pin-point an alternative that suits your needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!