Windows Timer Learning

Source: Internet
Author: User
Tags apc

A timer is a kernel object that is fired at a specific time or regular interval. Asynchronous program calls that combine timers allow the callback function to execute when any timer is fired.

You can create a timer by calling CreateWaitableTimer (), which returns a handle to the kernel object. If the timer already exists, you can get a process-related handle by using Openwaitabletimer (). The handle that is obtained either through CreateWaitableTimer () or through Openwaitabletimer () must be released when the timer is not required, by using the function CloseHandle ().

Timed time is set by calling SetWaitableTimer (), which can be set to a specific moment (e.g. December, 1999 at 9:45 PM) or a relative time (e.g. every five minutes from now). The time parameter of the function setwaitabletime () timing requires the Large_integer type. This value should conform to the format described in structure FILETIME. If the value is positive, it represents a specific moment. If the value is negative, it represents the relative time in 100 nanosecond units. You can also set the timer to be a periodic self-excitation by passing a period parameter (in milliseconds) to the third parameter of SetWaitableTimer (). Passing false in the second parameter of CreateWaitableTimer () can result in an auto-zeroing timer.

In all APC, the thread must be in the listening state to perform a complete routine. The complete routine will always be executed by the same thread that called SetWaitableTimer (), so this thread must be placed on its own listening state. You can call any of the following listener functions to complete the Listening state setting:
SleepEx ();
WaitForSingleObjectEx ();
WaitForMultipleObjectsEx ();
Msgwaitformultipleobjectsex ();
SignalObjectAndWait ();

The CreateWaitableTimer () function is used to create a waiting timer:

Function prototype handle WINAPI CreateWaitableTimer (lpsecurity_attributes lptimerattributes,bool bmanualreset,lpctstr lpTimerName ) Description lpsemaphoreattributessecurity_attributes, specifying a structure that sets the security characteristics of the object. If you declare a parameter as ByVal as Long and pass a value of 0, you can use the object's default security settings. Bmanualresetlong, if true, indicates the creation of a manual reset timer, or FALSE to create an automatic reset timer. Lpnamestring, which specifies the name of the waiting timer object. Use vbNullString to create an unnamed timer object. If a waiting timer with that name already exists, open the ready-to-wait timer directly. The name may not match the name of an existing mutex, event, signal machine, or file map.

The function Openwaitabletimer () is used to get a handle to an existing waiting timer that is associated with the current process:

HANDLE Openwaitabletimer (                      DWORD dwdesiredaccess,/* access rights */                      BOOL bInheritHandle,/* Whether to allow child processes to inherit the handle */                      LPCTSTR Lptimername/* Name of object to open */  

At the time of creation, the waiting timer object is always in the non-triggering state. When we want to trigger a timer, we must call the SetWaitableTimer () function:

BOOL SetWaitableTimer (                        HANDLE htimer,/* Timer to trigger */                        const LARGE_INTEGER *pduetime,/* Timer FIRST trigger time */                        long LP Eriod,/* After the first trigger, the timer's trigger frequency */                        ptimerapcroutine pfncompletionroutine,/* Asynchronous procedure Call APC function */                        LPVOID Lpargtocompletionroutine,/* The parameters of the APC function */                        BOOL fresume/* Continue execution, General pass false */  );

Note that the 2nd parameter Pduttime and the 3rd parameter lperiod to be used in conjunction, Pduttime is a lagre_integer structure pointer, indicating the time of the first notification, the time format is UTC (Standard time), is an absolute value, if you want to set a relative values, That is, let the wait timer after calling the SetWaitableTimer function for the first time after the notification, as long as a negative number is passed to the parameter, but the value must be a multiple of 100ns, that is, the unit is 100ns. The 3rd parameter indicates the time interval for future notifications, in milliseconds, which is 0 o'clock, which indicates that only the first notification is not notified later.

The function Cancelwaitabletimer is used to cancel the specified timer:
BOOL WINAPI Cancelwaitabletimer (HANDLE htimer);

Note that several of the functions mentioned above need to include the <windows.h> header file, but only those containing this header file will be prompted to compile:
' CreateWaitableTimer ' undefined; assuming extern returning int
You need to define a # define _WIN32_WINNT 0x0400 before the header file is included so that you can resolve problems that createwaitabletimer () cannot find.

Here is a simple example of what I have written:

#define _WIN32_WINNT 0x0400  #include <stdio.h>  #include <windows.h>  #define _second 10000000  void CALLBACK OnTimer (LPVOID Lpargtocompletionroutine, DWORD Dwtimerlowvalue, DWORD Dwtimerhighvalue)  {      printf ("alarm!\n");  }  int main ()  {      int i;      int iRet;      Large_integer Li;      HANDLE Htimer = CreateWaitableTimer (null, FALSE, NULL);      if (Htimer = = NULL)          return-1;      Li. QuadPart =-2 * _second;      IRet = SetWaitableTimer (Htimer, &li, N, OnTimer, NULL, FALSE);      if (IRet = = 0)          return-1;      for (i = 0; i < i++)      {          SleepEx (INFINITE, TRUE);      }    System ("PAUSE");     return 0;  }

Transfer from

Windows Timer Learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.