我在 Windows 10 上使用 Delphi XE7。
我已经使用下面的代码很长时间了,只是阅读了SetTimer(). 简单地说,我是从非 UI 线程设置计时器,但 Microsoft 的文档说它们只能在 UI 线程上设置。广泛的测试表明我的代码工作正常,但我不能相信我的系统的行为与其他系统相同,也不相信 Microsoft 文档 100% 准确。谁能验证一下这段代码是否OK?
Delphi代码不会死锁,它几乎只是调用SetTimer()(我知道有一个竞争条件设置TTimer.FEnabled)。
MSDN文档说:
hWnd
类型:HWND
与计时器关联的窗口句柄。该窗口必须由调用线程拥有。
我想要完成的是工作线程做一些事情,并且在适当的时候,它们通知主线程必须更新 UI 的元素,然后主线程更新 UI。我知道如何使用TThread.Synchronize(),但在某些情况下可能会发生死锁。我可以使用PostMessage()我的工作线程并在 UI 线程中处理消息。
Delphi中有没有其他方法来通知和更新UI线程?
unit FormTestSync;
interface
uses SysUtils, Classes, Forms, StdCtrls, ExtCtrls, Controls;
type
TypeThreadTest = class(TThread)
protected
procedure Execute; override;
end;
type
TForm1 = class(TForm)
timer_update: TTimer;
Label1: TLabel;
procedure timer_updateTimer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
m_thread: TypeThreadTest;
m_value: integer; …Run Code Online (Sandbox Code Playgroud)