c - 为什么 setTimer 不起作用?

标签 c visual-studio-2010 winapi timer

<分区>

我有以下程序。我想知道 setTimer 是如何工作的。所以,我写了一个程序,但无法理解为什么 TimerProc 函数没有被调用。为什么?还需要做什么来触发 setTimer/TimerProc。请帮忙。

#include <windows.h>
#include <stdio.h>

VOID CALLBACK TimerProc(
    HWND hwnd,  // handle of window for timer messages 
    UINT uMsg,  // WM_TIMER message
    UINT idEvent,   // timer identifier
    DWORD dwTime    // current system time
   ) {
      printf("from callback\n");

   }
int main(int argc, char *argv[])
{
   UINT timerid = SetTimer(NULL,1,1000,TimerProc);/*changed the time from 1 to 1000, but no effect*/
   printf("timerid %d\n",timerid); 
   int i,j;

      //delay loop, waiting for the callback function to be called
   for(j=0;j<0xffffffff;j++);
   /*{
   printf("%d\n", j);
   }*/

   printf("done \n");
  system("PAUSE");  
  return 0;
}

最佳答案

计时器通过 Windows 消息队列工作,您有一个控制台应用程序。

如果您创建一个基本的 Win32 应用程序以便获得一个窗口和一个消息循环,当 WM_TIMER 消息被 DefWndProc() 调用捕获时,它将在此处进行回调。当然你也可以自己捕获WM_TIMER。

关于c - 为什么 setTimer 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15406326/

相关文章:

visual-studio - 在 Visual Studio 的对象监 window 口中搜索文本

C++ winapi ListView NM_CUSTOMDRAW : not getting CDDS_ITEMPREPAINT

C 找出大数的质因数

c - 如何在 C 函数中返回超过 1 个值?

c - __FILE__ 在 .h 中它解析为什么

c - 在 while 循环中转义 scanf

c - 具有不需要的颜色的纹理

c++ - wxWidgets构建成功后下一步做什么: Visual Studio 2010

c++ - Visual C++ 链接器错误 2019

c - 安全移除安装的驱动器