void CALLBACK CheckCom(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
{ printf("Why NO Display!\n");}
int _tmain(int argc, _TCHAR* argv[])
{
SetTimer(NULL,1,1000,CheckCom); while(1)
{
Sleep(100);
}
return 0;
}
我是在控制台写的;为什么定时器回调函数没有执行呢?
{ printf("Why NO Display!\n");}
int _tmain(int argc, _TCHAR* argv[])
{
SetTimer(NULL,1,1000,CheckCom); while(1)
{
Sleep(100);
}
return 0;
}
我是在控制台写的;为什么定时器回调函数没有执行呢?
#include <stdio.h>VOID CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);int main(int argc, char* argv[])
{
SetTimer(NULL, 0, 2000, NULL); MSG msg; while (GetMessage(&msg, NULL, 0, 0))
{
switch (msg.message)
{
case WM_TIMER:
TimerProc(NULL, 0, 0, 0);
break;
default:
break;
}
} return 0;
}VOID CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
printf("Time Up!\n");
}
#include <windows.h>void CALLBACK CheckCom(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
{
printf("Why NO Display!\n");}
int main(int argc, char* argv[])
{ SetTimer(NULL,1111111,1000,CheckCom);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
if (msg.message == WM_TIMER)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (msg.message == WM_QUIT)
{
break;
}
}
return 0;
}
给个例子给你:
#include <windows.h>
#include <stdio.h>
#include <conio.h> int count =0; VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
}DWORD CALLBACK Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,TimerProc);
BOOL bRet;
while( (bRet = GetMessage(&msg,NULL,0,0))!= 0)
{
if(bRet==-1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}int main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application\n");
HANDLE hThread = CreateThread(NULL,0,Thread,0,0,&dwThreadId);
_getch();
return 0;
}
问题1:你没有GetMessage或者PeekMessage,你的SetTime没办法触发.
问题2:你本身进程是阻塞的,一直在循环Sleep();即使有,也没办法触发GetMessage吧.
请问有没有这样的API函数:假设这个定时器定的是1s,在没有到达1s的时候清空定时器;使其重新开始定时,有这样的API函数吗?
然后再 SetTimer