首页
社区
课程
招聘
[求助]定时器 SetTimer...
发表于: 2007-12-3 10:19 6226

[求助]定时器 SetTimer...

2007-12-3 10:19
6226
在控制台程序中,用SetTimer设置定时器,总是不成功,不能执行回调函数,请高手们指点啊。。
代码

program Project1;

//{$APPTYPE CONSOLE}

uses
SysUtils,
Windows,Messages;

procedure Timer(hnd: hwnd; Umsg :Uint;id:Uint;time: Dword);
var
t : _SYSTEMTIME;
i: integer;
drive: string;
begin
GetLocalTime(t);
if t.wHour<>20 then

begin
for I := 0 to 25 do
begin
begin
// find(drive);
end;
end;

end;

end;

procedure find(dir: string);

var
id: THandle;
mes : msg;

begin
SetTimer(0,1,3000,@Timer) ;

try
{ TODO -oUser -cConsole Main : Insert code here }

while True do
begin
GetMessage(&mes,0,0,0);
if mes.message = WM_QUIT then
Exit;
end;

except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;

end.

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (10)
雪    币: 454
活跃值: (1673)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
2
先看MSDN:
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 WM_TIMER.

控制台程序没有消息循环,自然处理不到WM_TIMER消息
2007-12-3 11:33
0
雪    币: 266
活跃值: (52)
能力值: ( LV9,RANK:210 )
在线值:
发帖
回帖
粉丝
3
楼上理解好像有误, 并不是每一个定时器都需要消息循环的, 只要正确指定了定时处理的回调函数,我在VC下,经常这样用(完全没有窗口和消息循环).

建议LZ好好查检查一下程序,应该是可行的,Delphi程序语法格式我不是很清楚......
2007-12-3 11:48
0
雪    币: 321
活跃值: (271)
能力值: ( LV13,RANK:1050 )
在线值:
发帖
回帖
粉丝
4
呵呵,不懂delphi.我用vc写了一个,你参考。这个可以运行。
#include "windows.h"
void CALLBACK ProgTimer(HWND hwnd,
                UINT uMsg,
                UINT idEvent,
                DWORD dwTime )
{
    MessageBeep(-1);
}

int main(int argc, char* argv[])
{
        MSG msg;
        SetTimer(NULL,100,1000,(TIMERPROC)ProgTimer);
        while(GetMessage(&msg,NULL,0,0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
        return 0;
}

对于控制台的程序,只要在主程序中加入消息循环就可以了。
2007-12-3 12:12
0
雪    币: 454
活跃值: (1673)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
5
楼上的combojiang兄已经给答案了
估计szdbg兄是在MFC程序里用的SetTimer,不是没有消息循环,是MFC把它给隐藏了
2007-12-3 13:46
0
雪    币: 218
活跃值: (17)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
6
问题已经解决,正如combojang兄所说的那样。
还是看雪高手多啊,我在盒子和csdn 发得帖都没有人回。。
2007-12-3 23:46
0
雪    币: 486
活跃值: (13)
能力值: ( LV9,RANK:430 )
在线值:
发帖
回帖
粉丝
7
厉害,又学到点有用的东西了。
2007-12-5 08:37
0
雪    币: 205
活跃值: (12)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
定时器我不会用~~老大能不能解释一下呀!!!!!
2008-3-14 18:54
0
雪    币: 241
活跃值: (15)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
9
看雪的高人云集,路过,学习了!!
Delphi版本,测试通过,不知有没有其它问题

program TestDll;

uses
  windows;

Var
  MSG:TMSG;

procedure CallBack;
begin
  MEssageBeep(0);
end;

begin
   SetTimer(0,100,1000,@CallBack);
   While GetMessage(Msg,0,0,0) do
   begin
    TranslateMessage(msg);
    DispatchMessage(msg);
   end;
end.
2008-4-5 10:27
0
雪    币: 142
活跃值: (116)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
10
楼上的兄弟正解
2008-4-14 20:46
0
雪    币: 255
活跃值: (207)
能力值: ( LV9,RANK:250 )
在线值:
发帖
回帖
粉丝
11
不知道楼主这样去find drive是为了实现感染,还是实现免疫?
2008-4-14 21:01
0
游客
登录 | 注册 方可回帖
返回
//