我想在dll中实现实现定时器(settimer)但不能实现,偶在有form的程序里可以实现,请问各位大虾怎么弄??或者
在dll 实现定时器功能?最好能给出code,谢谢!(在网上看了三种方法但都没实现,很是郁闷)

解决方案 »

  1.   

    如果DLL内部没有独立的线程,或者常驻窗体,那么就没办法实现。
      

  2.   

    那意思是我在dll中创建一个线程来实现settimer的功能? 楼上兄弟能给写个例子么?
      

  3.   

    你可以看一下delphi里的timer控件,里面会建立一个虚拟的窗口来接收消息,你也照着写一个就OK了,
      

  4.   

    type
      TMyThread = class(TThread)
        private
          FTimer:TTimer;
          procedure TimerProc(Sender: TObject);
        protected
          procedure Execute;override;
        public
          constructor Create;override;
          procedure KillIt;//结束
      end;
    implementation
      procedure TMyThread.TimerProc(Sender: TObject);
      begin
        ...
      end;
      procedure TMyThread.KillIt;
      begin
        PostMessage(Handle,WM_QUIT,0,0);
      end;
    procedure TMyThread.Execute;
    begin
      FTimer:=TTimer.Create(nil);
      try
        try
          FTimer.Enabled:=false;//需要用的时候Enabled设置为True即可。
          FTimer.OnTimer:=TimerProc;
          while GetMessage(MsgRec, 0, 0, 0) do begin
            TranslateMessage(MsgRec);
            DispatchMessage(MsgRec)
          end;
          Terminate;
        finally
          FSocket.Free;
        end;
      finally
        FTimer.Free;
      end;
    end;
    要结束的话,往线程的Handle发送一个WM_QUIT消息,本例当中调用KillItvar
      T: TMyThread;
    begin
      ...
      T.KillIt;
    end;
      

  5.   

    貌似在群里给过你例子的,settimer的俺是药...
      

  6.   

    哪有这种事
    procedure TimerProc(hwnd:HWND;uMsg:UINT; idEvent :UINT_PTR;dwTime:DWORD);stdcall;
    begin
       //add your code.
    end;
    //....begin
    settimer(0,0,1000,@TimerProc);
    end.
      

  7.   

    记得好象是可以不需要window的.
      

  8.   


    type
      TTimeProcCall = procedure(hand: THandle; Msg, Identer, dwTime: DWord) of object; stdcall; //定时器函数指针
    function TMyTime.CreateTime(Time: Dword; Porc: TTimeProcCall): boolean; //创建
    begin
      Result := Windows.SetTimer(FHwnd,Dword(Self), Time, @Porc) <> 0;
    end;
    function TMyTime.FreeTime: boolean;//销毁
    begin
      Result := KillTimer(FHwnd, Dword(Self));
    end;
    procedure TMyTime.TimeProc(hand: THandle; Msg, Identer, dwTime: DWord); stdcall; //回调过程
    begin
    ....//TimeProc
    end;
      

  9.   

    窗口不一定要显示出来,但是需要通过CreateWindow来生成,从而可以响应消息,并不一定是一个TForm,如此只需要所在的线程有实现消息循环,即可以响应WM_TIMER,这也就是我所说的常驻窗体。由于WM_TIMER是发到SetTime调用的线程当中,所以也可以通过一个独立的线程去处理该消息,比如GetMessage等等。
      

  10.   


    UINT_PTR SetTimer(          HWND hWnd,
        UINT_PTR nIDEvent,
        UINT uElapse,
        TIMERPROC lpTimerFunc
    );
    ParametershWnd
    [in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.hwnd参数可以传入NULL,所以,不一定需要window
      

  11.   

    是可以传入NULL,如果没有独立线程的话,就永远运行不到你的执行代码。