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;
}
我是在控制台写的;为什么定时器回调函数没有执行呢?

解决方案 »

  1.   

    http://topic.csdn.net/u/20070111/11/7f87bc71-a00e-4def-864b-c193b8adf732.html
      

  2.   

    你没有GetMessage吧#include <Windows.h>
    #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");
      

  3.   

    消息循环还没有开始。。你就使用 SetTimer了?
      

  4.   

    #include <stdio.h>
    #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;
    }
      

  5.   

    定时器是基于WM_TIMER消息的,没有消息循环,你的程序收不到定时器消息,所以不行。
    给个例子给你:
    #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;
    }   
      

  6.   

    楼主,我觉得你有2个问题
    问题1:你没有GetMessage或者PeekMessage,你的SetTime没办法触发.
    问题2:你本身进程是阻塞的,一直在循环Sleep();即使有,也没办法触发GetMessage吧.
      

  7.   

    谢谢各位的回答,问题已解决!
    请问有没有这样的API函数:假设这个定时器定的是1s,在没有到达1s的时候清空定时器;使其重新开始定时,有这样的API函数吗?
      

  8.   

    先KillTimer
    然后再 SetTimer