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,创建成功了,就是不执行代码,为什么呢.

解决方案 »

  1.   

    SetTimer是基于消息循环的,不处理消息循环就捕捉不到事件
      

  2.   

    那意思就是说在DLL里使用SetTimer的话就要处理系统消息了,如果不处理就不效吗?但是为什么有时候执行了,有时候没执行呢.
      

  3.   

    library Project2;
    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
    没有执行,为什么会这样呢?
      

  4.   

    SetTimer 是要 wnd handle 得。
      

  5.   


    //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;
      

  6.   

    传递一个参数HWND给settimer就行了
    我也有一个DLL用到settimer,只是随便传递主程序的一个HWND就没问题了
      

  7.   

    呵呵,没有exe的只有一个DLL文件也有HWND?
      

  8.   

    When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing
    你调用SetTimer的那个线程必须有消息循环
      

  9.   

    在我这里测试正常,我把INTERVAL设置的短了点,调长点试一下
      

  10.   

    简单事情咋~
    用Classes.AllocateWnd分配一个消息队列句柄就可以处理了具体自己找资料~
      

  11.   

    楼主,我也不清楚问题的是怎么回事。
    如果你实在没办法,试试在某个相对独立的线程作个时间判断,取代SetTimer吧。
    相信你的SetTimer也是想周期性检测执行某些东西。