可等待计时器内核对象的使用(CreateWaitableTimer)



The CreateWaitableTimer function creates or opens a waitable timer object.

HANDLE CreateWaitableTimer(
  LPSECURITY_ATTRIBUTES lpTimerAttributes, // SD
  BOOL bManualReset,                       // reset type
  LPCTSTR lpTimerName                      // object name
);

BOOL SetWaitableTimer(
  HANDLE hTimer,                          // handle to timer
  const LARGE_INTEGER *pDueTime,          // timer due time
  LONG lPeriod,                           // timer interval
  PTIMERAPCROUTINE pfnCompletionRoutine,  // completion routine
  LPVOID lpArgToCompletionRoutine,        // completion routine parameter
  BOOL fResume                            // resume state);

Parameters
hTimer
[in] Handle to the timer object. The CreateWaitableTimer or OpenWaitableTimer function returns this handle.
pDueTime
[in] Specifies when the state of the timer is to be set to signaled, in 100 nanosecond intervals. Use the format described by the FILETIME structure. Positive values indicate absolute time. Be sure to use a UTC-based absolute time, as the system uses UTC-based time internally. Negative values indicate relative time. The actual timer accuracy depends on the capability of your hardware. For more information about UTC-based time, see System Time.
lPeriod
[in] Specifies the period of the timer, in milliseconds. If lPeriod is zero, the timer is signaled once. If lPeriod is greater than zero, the timer is periodic. A periodic timer automatically reactivates each time the period elapses, until the timer is canceled using the CancelWaitableTimer function or reset using SetWaitableTimer. If lPeriod is less than zero, the function fails.
pfnCompletionRoutine
[in] Pointer to an optional completion routine. The completion routine is application-defined function of type PTIMERAPCROUTINE to be executed when the timer is signaled. For more information on the timer callback function, see TimerAPCProc.
lpArgToCompletionRoutine
[in] Pointer to the structure that is passed to the optional completion routine.
fResume
[in] Specifies whether to restore a system in suspended power conservation mode when the timer state is set to signaled. If fResume is TRUE on a platform that does not support a restore, the call will succeed, but GetLastError returns ERROR_NOT_SUPPORTED.
Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

使用案例:

以绝对时间运行:

HANDLE hTimer;
SYSTEMTIMES st;
FILETIME ftLocal,ftUTC;
LARGE_INTEGER liUTC;
hTime=CreateWaitableTimer(NULL,FALSE,NULL);

st.wYear=2008;
st.wMonth=1;
st.wDayOfWeek=0;
st.wDay=1;
st.wHour=13;
st.wMinute=0;
st.wSecond=0;
st.wMilliseconds=0;

SystemTimeToFileTime(&st,&ftLocal);
LocalFileTimeToFileTime(&ftLocal,&ftUTC);
liUTC.LowPart=ftUTC.dwLowDateTime;
liUTC.HighPart=ftUTC.dwHighDateTime;

SetWaitableTimer(hTimer,&liUTC,6*60*60*1000,NULL,NULL,FALSE);

以相对时间运行:

HANDLE hTimer;
LARGE_INTEGER li;

hTimer=CreateWaitableTimer(NULL,FALSE,NULL);
const int nTimerUnitsPerSecond=10000000;//1s=1000ms=1000 000us=10 000 000 * 100ns
li.QuadPart=-(5*nTimerUnitsPerSecond);//5s 后执行

SetWaitableTimer(hTimer,&li,6*60*60*1000,NULL,NULL,FALSE);

取消(非关闭)定时器内核对象:

BOOL CancelWaitableTimer( HANDLE hTimer );




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值