procedure TimerDo(hWnd: HWND; uMsg: UINT; idEvent: UINT; dwTime: DWORD); stdcall;
begin
//...............执行
end;
begin
iTimerID := SetTimer(0, 0, 3000, @TimerDo);
end;
我在DLL中使用SetTimer创建定时器为什么有时候会执行有时候不会执行? iTimerID也不是0,创建成功了,就是不执行代码,为什么呢.
begin
//...............执行
end;
begin
iTimerID := SetTimer(0, 0, 3000, @TimerDo);
end;
我在DLL中使用SetTimer创建定时器为什么有时候会执行有时候不会执行? iTimerID也不是0,创建成功了,就是不执行代码,为什么呢.
uses
SysUtils,
Classes,
Unit1 in 'Unit1.pas';
{$R *.res}
begin
xzcx;
end.unit Unit1;
interface
uses Windows,SysUtils;
var
t1: LongWord=0; // 定时器句柄
procedure xzcx();
procedure TimerOn; // 安装定时器
procedure TimerOff; // 删除定时器
implementation
Procedure WriteTxt(sFileName,sText:String); //写txt文件
Var
F:TextFile;
Begin
AssignFile(F,sFileName);
IF FileExists(sFileName) Then
Append(F)
Else
ReWrite(F);
WriteLn(F,sText);
CloseFile(F);
End;
procedure TimerProc(hWnd: HWND; uMsg: UINT; idEvent: UINT; dwTime: DWORD); stdcall;
begin
WriteTxt('0.txt','正在执行定时器'+ inttostr(t1));
end;
procedure xzcx();
begin
TimerOn;
WriteTxt('0.txt','启动定时器'+ inttostr(t1));
end;
procedure TimerOn;
begin
if (t1 <> 0) then TimerOff;
t1:= SetTimer(0, 0, 100, @TimerProc);
end;
procedure TimerOff;
begin
if (t1 <> 0) then
begin
KillTimer(0, t1);
t1 := 0;
end;
end;
end.结果
启动定时器28535
启动定时器32759
没有执行,为什么会这样呢?
//DLL
unit Unit1;interfaceuses
Windows,SysUtils,Dialogs;var
I:Integer=0 ;
TimerHandle: LongWord;procedure MyTimerProc(hWnd: HWND; uMsg: UINT; idEvent: UINT; Time: DWORD);
procedure TimerOn;stdcall ;implementationprocedure MyTimerProc(hWnd: HWND; uMsg: UINT; idEvent: UINT; Time: DWORD);
begin
ShowMessage('OK');
end;
//=============
procedure TimerOn;stdcall;
var
SubMessage: TMsg;
begin
TimerHandle := SetTimer(0, 0, 30, @MyTimerProc);
while GetMessage(SubMessage, 0, 0, 0) do DispatchMessage(SubMessage);//用这条语句接收并分发一下消息
KillTimer(TimerHandle, 0);
end;
//=============
end.
---------------------------------------
Project1.dprexports
TimerOn;
我也有一个DLL用到settimer,只是随便传递主程序的一个HWND就没问题了
你调用SetTimer的那个线程必须有消息循环
用Classes.AllocateWnd分配一个消息队列句柄就可以处理了具体自己找资料~
如果你实在没办法,试试在某个相对独立的线程作个时间判断,取代SetTimer吧。
相信你的SetTimer也是想周期性检测执行某些东西。