关于windows的CreateTimerQueueTimer函数

luciferisnotsatan 2014-12-02 11:25:22

#include "stdafx.h"

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


VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
printf("World!\n");
}


int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hTimer = NULL;
HANDLE hTimerQueue = NULL;
int arg = 123;

// Create the timer queue.
hTimerQueue = CreateTimerQueue();
if (NULL == hTimerQueue)
{
printf("CreateTimerQueue failed (%d)\n", GetLastError());
return 2;
}

// Set a timer to call the timer routine in 1 seconds.
if (!CreateTimerQueueTimer( &hTimer, hTimerQueue,
(WAITORTIMERCALLBACK)TimerRoutine, &arg , 200, 1000, WT_EXECUTEDEFAULT))
{
printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
return 3;
}

while(1)
{
printf("Hello!\n");
Sleep(1000);
}
DeleteTimerQueueTimer(hTimerQueue,hTimer,NULL);
// Delete all timers in the timer queue.
if (!DeleteTimerQueue(hTimerQueue))
printf("DeleteTimerQueue failed (%d)\n", GetLastError());

return 0;
}


上面的程序运行后,正常情况下,每一秒会输出一个hello,一个world。但把进程挂起一段时间,再恢复。发现会输出一串的“World!”。
个人感觉,就算把进程挂起了,用CreateTimerQueueTimer设置的timer还是在走。等进程恢复后,会收到一串的timer触发事件。

我的问题是,有没有方法做到在创建这个timer的进程(线程)挂起的同时,暂停这个timer?等进程恢复时,再一起恢复timer。


欢迎大家踊跃回复,300分的帖子别又让赵老师一人独得了。
...全文
1001 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
orange4reg 2016-12-25
  • 打赏
  • 举报
回复
WT_EXECUTEINTIMERTHREAD 试试加上这个再使用,这个解释是在创建timer的线程中运行,系统不会再创建新的线程。
  • 打赏
  • 举报
回复
TimerQueue是内核对象,跟你进程挂没挂起没关系。
图灵狗 2014-12-02
  • 打赏
  • 举报
回复
换一条思路,在回调函数TimerRoutine中根据信号量来决定是否执行代码,这样也可以达到同样的效果。
luciferisnotsatan 2014-12-02
  • 打赏
  • 举报
回复
引用 4 楼 luciferisnotsatan 的回复:
感觉这个函数挺坑的,有没有人在项目中用到过这种timer?谈谈感受
就我贴出的例子,等待一会再resume,发现多出好几个ntdll.dll!TpCallbackIndependent+0x238 (wait wrqueue) 线程。
luciferisnotsatan 2014-12-02
  • 打赏
  • 举报
回复
感觉这个函数挺坑的,有没有人在项目中用到过这种timer?谈谈感受
ZG 2014-12-02
  • 打赏
  • 举报
回复
暂停?suspend时直接delete掉,然后再create?
luciferisnotsatan 2014-12-02
  • 打赏
  • 举报
回复
引用 1 楼 turingo 的回复:
换一条思路,在回调函数TimerRoutine中根据信号量来决定是否执行代码,这样也可以达到同样的效果。
这个是公司项目中遇到的。最近更新了下模拟器(模拟器是国外team做的,我这边看不到代码),然后调试代码时,命中断点,过会再go,会卡一会。 经过观察,发现在挂起进程一段时间后再恢复,有一个线程会占满一个CPU核一段时间。然后看看这个线程的栈,发现一些函数名带callback,time periodic, sensor等字眼。因此推测,可能是新版本用了CreateTimerQueueTimer来模拟一些传感器信号导致的。

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧