win32控制台应用程序怎样能够定时(每隔2秒)执行一段代码

不要复制粘贴哦
2025-06-23 08:15:38
推荐回答(3个)
回答1:

//要做其他事情可以自定义消息, 然后在消息循环那里加入对应的消息处理, 通
//过PostMessage/SendMessage来传消息```
#include
#include

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");
}

回答2:

while (true)
{
// 要运行的东西写这里
Sleep(2000);
}

回答3:

搜索 WM_TIMER