RTX64 Timer and Sleep Calls Programming Example

The following example demonstrates the use of RTX64 timer and sleep calls.

#include "windows.h"
#include "stdio.h"
#include "rtapi.h"
//
// Local data.
//
LARGE_INTEGER Period; // Timer period
LARGE_INTEGER StartTime; // Start time of sampling run
ULONG TimerCounter = 0; // Counts entries to timer handler
//
//
VOID
main(int argc, char **argv)
{
      LARGE_INTEGER x;
      HANDLE hTimer;
      //
      // Set default timer period to 500 micro seconds
      //
      Period.QuadPart = 5000;
      //
      // Set to the next to highest real-time priority
      //
      if (!RtSetThreadPriority( GetCurrentThread(), RT_PRIORITY_MAX-1))
            printf("WARNING: Can't set to highest RTAPI priority.\n");
      //
      // Setup and start the periodic timer.
      //
      if (!(hTimer = RtCreateTimer(
            NULL, // Security - NULL is none
            0, // Stack size - 0 is use default
            TimerHandler, // Timer handler
            NULL, // NULL context (argument to handler)
            RT_PRIORITY_MAX, // Priority
            CLOCK_2))) // RTX64 HAL Timer
      {
            printf("ERROR: Could not create the RTAPI timer.\n");
            ExitProcess(2);
      }
      if (!RtSetTimerRelative( hTimer, &Period, &Period))
      {
            printf("ERROR: Could not set and start the RTAPI timer.\n");
            ExitProcess(2);
      }
      //
      // Wait for the sampling time.
      //
      Sleep(1000);
      //
      // Stop and delete the timer.
      //
      RtCancelTimer( hTimer, &x);
      RtDeleteTimer( hTimer);
      printf(" Value of number of timers counter is %d\n",TimerCounter);
      ExitProcess(0);
}
//
// timer handler function
// - increment the tick counter
//
int
RTFCNDCL
TimerHandler(
PVOID unused
)
{
      TimerCounter++;
      return 0 ;
}

See Also

Clocks and Timers