WaitableTimer APC

void test_waitabletimer()
{
	//创建一个
	HANDLE hTimer = CreateWaitableTimer(0, FALSE, NULL);
	SYSTEMTIME st = {0};
	GetLocalTime(&st);
	_tprintf(TEXT("系统时间:%d - %d - %d %d:%d : %d\n"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);

	//准备2个时间对象 . Timer需要UTC时间
	FILETIME ft_local, ft_utc;
	//转时间
	SystemTimeToFileTime(&st, &ft_local);
	//utc时间
	LocalFileTimeToFileTime(&ft_local, &ft_utc);
	//也可以 : ft_utc.dwHighDateTime<<32 | ft_utc.dwLowDateTime
	LARGE_INTEGER li_utc;
	li_utc.LowPart = ft_utc.dwLowDateTime;
	li_utc.HighPart = ft_utc.dwHighDateTime;

	//从当前时间就触发一次, 接着2秒触发一次
	BOOL bSuccess = SetWaitableTimer(hTimer, &li_utc, 2000, NULL, NULL, FALSE);
	if (!bSuccess){
		_tprintf(TEXT("SetWaitableTimer 失败 ,err:%d\n"), GetLastError());
		CloseHandle(hTimer);
		return;
	}
	for (int i = 0; i < 5; ++i){
		printf("waiting!\n");
		if (!WaitForSingleObject(hTimer, -1)){
			_tprintf(TEXT("ok \n"));
		}
	}
	CloseHandle(hTimer);
}

 

 

APC : 可以把下面的例子修改成一个线程  , 把 for 修改成 while(1)  这样, 就有一个定时器线程了

void test_waittimer2()
{
	HANDLE hTimer = CreateWaitableTimer(0, FALSE, NULL);

	//如果是负数 , 则是相对当前的时间 . 
	LARGE_INTEGER li;
	li.QuadPart = -3 * 10000000;   //过3秒

	//过3秒触发, 之后每3秒触发一次
	SetWaitableTimer(hTimer, &li, 3000, TimerAPCProc, NULL, NULL);

	for (int i = 0; i < 5; ++i)
	{
		printf("waiting!\n");
		SleepEx(INFINITE, TRUE);
		printf("index:%d\n", i);
	}
	CancelWaitableTimer(hTimer);
	CloseHandle(hTimer);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值