settimer没响应
main(){
SetTimer(0,1,1000,TimerProc);
getch();
}
void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT TimerId,DWORD dwTime)
{
HWND ihwnd=FindWindow(NULL,"tm2009");
printf("%d",ihwnd);//测试是否响应
if(ihwnd)
{
SendMessage(ihwnd,WM_QUIT,0,0);
}
}
代码执行,但没响应,不知道哪里出错,,,求教
??发贴要验证码了? 本帖最后由 whypro 于 2010-5-25 16:03 编辑
没有消息循环呀!
WM_QUIT:
PostQuitMessage会发送WM_QUIT给消息队列。注意,WM_QUIT永远不会到达窗口过程,因为GetMessage得到WM_QUIT后就会返回FALSE,从而结束消息循环,最后进程结束,程序退出。 SetTimer的作用不就是有循环做用的吗? 每秒循环一次 楼主你回去好好看看消息循环那章! 1// testtimer.cpp : 定义控制台应用程序的入口点。
2//
3
4#include "stdafx.h"
5#include <windows.h>
6#include <conio.h>
7
8static UINT idTimer = 0;
9static int reentry = 0;
10static int call_cnt = 0;
11
12void LengthyWork(void)
13{
14 //Sleep(3000);
15 int i = 0,j = 0;
16 for(i;i < 50000;) {
17 i++;
18 for(j = i;j > 0;) {
19 j--;
20 }
21 }
22
23}
24VOID CALLBACK OnTimer(HWND hwnd,
25 UINT uMsg,
26 UINT_PTR idEvent,
27 DWORD dwTime
28)
29{
30 ++call_cnt;
31 printf("entry(%d)reentry:%d\n",call_cnt,reentry);
32 ++reentry;
33 LengthyWork();
34 --reentry;
35 printf("exit(%d) reentry:%d\n",call_cnt,reentry);
36}
37int _tmain(int argc, _TCHAR* argv[])
38{
39 idTimer = SetTimer(NULL,0,1000,OnTimer);
40 int ret = 0;
41 MSG msg;
42 while(1) {
43 if(kbhit()) {
44 return 0;
45 }
46 ret = GetMessage(&msg,NULL,0,0);
47 if(ret) {
48 TranslateMessage(&msg);
49 DispatchMessage(&msg);
50 }
51 }
52 return 0;
53}
54
一次执行结果如下:
entry(1)reentry:0
exit(1) reentry:0
entry(2)reentry:0
exit(2) reentry:0
entry(3)reentry:0
exit(3) reentry:0
entry(4)reentry:0
exit(4) reentry:0 呵呵,实在是看不懂,回头来还是用while语句做....
谢谢whypro的耐心/:good 找本windows书,看看消息循环,弄懂time与整体消息循环的关系!
页:
[1]