实验:设定一个已经过期的时间作为初始值,查看:
1。Timer是否能够立刻激活
2。如果Timer还有周期激活的特性,那么周期应该怎么算呢,是从已经过期的时间开始算起?还是从设定timer的当前时刻算起?
/*
本代码的意图在于查看如果你给Timer设定一个已经过去的时间,那么:
1. Timer句柄会不会被激活
2. Timer会表现出什么样的特征
*/
#include "stdafx.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
/*定义一些变量*/
HANDLE hTimer;
SYSTEMTIME stYearsAgo;
SYSTEMTIME stNow;
FILETIME ftYearsAgo;
FILETIME ftYearsAgoUTC;
LARGE_INTEGER liTimeGoOff;
ZeroMemory(&stYearsAgo,sizeof(stYearsAgo));
unsigned int nInterval = 10*60*1000;
/*将时间设定为2011年10月13日正午12时整,间隔为10分钟一次*/
stYearsAgo.wYear = 2011;
stYearsAgo.wMonth = 10;
stYearsAgo.wDay = 13;
stYearsAgo.wHour = 12;
::SystemTimeToFileTime(&stYearsAgo,&ftYearsAgo);
::LocalFileTimeToFileTime(&ftYearsAgo,&ftYearsAgoUTC);
liTimeGoOff.HighPart = ftYearsAgoUTC.dwHighDateTime;
liTimeGoOff.LowPart = ftYearsAgoUTC.dwLowDateTime;
/*设定定时器,查看定时器是否可以被激活,激活后多长时间再被激活(10分钟?还是别的?)*/
hTimer = ::CreateWaitableTimerA(NULL,FALSE,NULL);
::SetWaitableTimer(hTimer,&liTimeGoOff,nInterval,NULL,NULL,FALSE);
while(WaitForSingleObject(hTimer,INFINITE) == WAIT_OBJECT_0)
{
::GetLocalTime(&stNow);
printf("%02u:%02u:%02u - Timer goes off!\n",stNow.wHour,stNow.wMinute,stNow.wSecond);
}
return 0;
}
结论:
1。可以立刻激活
2。按照过期时间计算(10分钟一次,于是1点30就再度signal,而不是1点38)

本文探讨了在Windows环境下,通过设置一个已经过期的时间作为定时器的初始值,观察定时器是否能立即激活及周期计算方式。实验结果显示,定时器能够立即激活,并且周期计算是从设定的时间开始算起,而非从创建定时器的当前时刻。
205

被折叠的 条评论
为什么被折叠?



