# PTimer Class Reference

A class representing a system timer. More...

`#include <timer.h>`

Inheritance diagram for PTimer:

List of all members.

## Public Types

typedef unsigned | IDType

## Public Member Functions

IDType | GetTimerId () const
PAtomicInteger::IntegerType | GetNextSerialNumber ()
Construction
| PTimer (long milliseconds=0, int seconds=0, int minutes=0, int hours=0, int days=0)
| Create a new timer object and start it in one shot mode for the specified amount of time.
| PTimer (const PTimeInterval &time)
| PTimer (const PTimer &timer)
PTimer & | operator= (DWORD milliseconds)
| Restart the timer in one shot mode using the specified time value.
PTimer & | operator= (const PTimeInterval &time)
PTimer & | operator= (const PTimer &timer)
virtual | ~PTimer ()
| Destroy the timer object, removing it from the applications timer list if it was running.
Control functions
virtual void | SetInterval (PInt64 milliseconds=0, long seconds=0, long minutes=0, long hours=0, int days=0)
| Set the value of the time interval.
void | RunContinuous (const PTimeInterval &time)
| Start a timer in continous cycle mode.
void | Stop (bool wait=true)
| Stop a running timer.
PBoolean | IsRunning () const
| Determine if the timer is currently running.
void | Pause ()
| Pause a running timer.
void | Resume ()
| Restart a paused timer continuing at the time it was paused.
PBoolean | IsPaused () const
| Determine if the timer is currently paused.
void | Reset ()
| Restart a timer continuing from the time it was initially.
const PTimeInterval & | GetResetTime () const
| Get the time this timer was set to initially.
Notification functions
virtual void | OnTimeout ()
| This function is called on time out.
const PNotifier & | GetNotifier () const
| Get the current call back function that is called whenever the timer expires.
void | SetNotifier (const PNotifier &func)
| Set the call back function that is called whenever the timer expires.
Member access
PInt64 | GetMilliSeconds () const
| Return number of milliseconds left in timer.
PInt64 | GetAbsoluteTime () const
| Return absolute time timer will expire.

## Static Public Member Functions

Global real time functions
static PTimeInterval | Tick ()
| Get the number of milliseconds since some arbtrary point in time.
static unsigned | Resolution ()
| Get the smallest number of milliseconds that the timer can be set to.

## Detailed Description

A class representing a system timer.

The time interval ancestor value is the amount of time left in the timer.

A timer on completion calls the virtual function `OnTimeout()`. This will in turn call the callback function provided by the instance. The user may either override the virtual function or set a callback as desired.

A list of active timers is maintained by the applications `PProcess` instance and the timeout functions are executed in the context of a single thread of execution. There are many consequences of this: only one timeout function can be executed at a time and thus a user should not execute a lot of code in the timeout call-back functions or it will dealy the timely execution of other timers call-back functions.

Also timers are not very accurate in sub-second delays, even though you can set the timer in milliseconds, its accuracy is only to -0/+250 ms. Even more (potentially MUCH more) if there are delays in the user call-back functions.

Another trap is you cannot destroy a timer in its own call-back. There is code to cause an assert if you try but it is very easy to accidentally do this when you delete an object that contains an onject that contains the timer!

Finally static timers cause race conditions on start up and termination and should be avoided.

## Member Typedef Documentation

typedef unsigned PTimer::IDType

## Constructor & Destructor Documentation

PTimer::PTimer | ( | long | milliseconds = `0`,
|  | int | seconds = `0`,
|  | int | minutes = `0`,
|  | int | hours = `0`,
|  | int | days = `0` |
| ) |  |  |

Create a new timer object and start it in one shot mode for the specified amount of time.

If the time was zero milliseconds then the timer is {not} started, ie the callback function is not executed immediately.

Parameters:

  milliseconds | Number of milliseconds for timer.
  seconds | Number of seconds for timer.
  minutes | Number of minutes for timer.
  hours | Number of hours for timer.
  days | Number of days for timer.

PTimer::PTimer | ( | const PTimeInterval & | time | ) |

Parameters:

  time | New time interval for timer.

PTimer::PTimer | ( | const PTimer & | timer | ) |

Parameters:

  timer | Timer to copy.

virtual PTimer::~PTimer | ( |  | ) | `[virtual]`

Destroy the timer object, removing it from the applications timer list if it was running.

## Member Function Documentation

PInt64 PTimer::GetAbsoluteTime | ( |  | ) | const`[inline]`

Return absolute time timer will expire.

PInt64 PTimer::GetMilliSeconds | ( |  | ) | const`[virtual]`

Return number of milliseconds left in timer.

Reimplemented from PTimeInterval.

PAtomicInteger::IntegerType PTimer::GetNextSerialNumber | ( |  | ) | `[inline]`

const PNotifier& PTimer::GetNotifier | ( |  | ) | const

Get the current call back function that is called whenever the timer expires.

This is called by the `OnTimeout()` function.

Returns:
  current notifier for the timer.

const PTimeInterval& PTimer::GetResetTime | ( |  | ) | const

Get the time this timer was set to initially.

IDType PTimer::GetTimerId | ( |  | ) | const`[inline]`

PBoolean PTimer::IsPaused | ( |  | ) | const

Determine if the timer is currently paused.

Returns:
  true if timer paused.

PBoolean PTimer::IsRunning | ( |  | ) | const

Determine if the timer is currently running.

This really is only useful for one shot timers as repeating timers are always running.

Returns:
  true if timer is still counting.

virtual void PTimer::OnTimeout | ( |  | ) | `[virtual]`

This function is called on time out.

That is when the system timer processing decrements the timer from a positive value to less than or equal to zero. The interval is then reset to zero and the function called.

Please note that the application should not execute large amounts of code in this call back or the accuracy of ALL timers can be severely impacted.

The default behaviour of this function is to call the `PNotifier` callback function.

PTimer& PTimer::operator= | ( | const PTimer & | timer | ) |

Parameters:

  timer | New time interval for timer.

PTimer& PTimer::operator= | ( | const PTimeInterval & | time | ) |

Parameters:

  time | New time interval for timer.

PTimer& PTimer::operator= | ( | DWORD | milliseconds | ) |

Restart the timer in one shot mode using the specified time value.

If the timer was already running, the "time left" is simply reset.

Returns:
  reference to the timer.

Parameters:

  milliseconds | New time interval for timer.

void PTimer::Pause | ( |  | ) |

Pause a running timer.

This differs from the `Stop()` function in that the timer may be resumed at the point that it left off. That is time is "frozen" while the timer is paused.

void PTimer::Reset | ( |  | ) |

Restart a timer continuing from the time it was initially.

static unsigned PTimer::Resolution | ( |  | ) | `[static]`

Get the smallest number of milliseconds that the timer can be set to.

All actual timing events will be rounded up to the next value. This is typically the platforms internal timing units used in the `Tick()` function.

Returns:
  minimum number of milliseconds per timer "tick".

void PTimer::Resume | ( |  | ) |

Restart a paused timer continuing at the time it was paused.

The time left at the moment the timer was paused is the time until the next call to the notification function.

void PTimer::RunContinuous | ( | const PTimeInterval & | time | ) |

Start a timer in continous cycle mode.

Whenever the timer runs out it is automatically reset to the time specified. Thus, it calls the notification function every time interval.

virtual void PTimer::SetInterval | ( | PInt64 | milliseconds = `0`,
|  | long | seconds = `0`,
|  | long | minutes = `0`,
|  | long | hours = `0`,
|  | int | days = `0` |
| ) |  |  | `[virtual]`

Set the value of the time interval.

The time interval, in milliseconds, is the sum of all of the parameters. For example all of the following are equivalent:

                  SetInterval(120000)
                  SetInterval(60000, 60)
                  SetInterval(60000, 0, 1)
                  SetInterval(0, 60, 1)
                  SetInterval(0, 0, 2)

Parameters:

  milliseconds | Number of milliseconds for interval.
  seconds | Number of seconds for interval.
  minutes | Number of minutes for interval.
  hours | Number of hours for interval.
  days | Number of days for interval.

Reimplemented from PTimeInterval.

void PTimer::SetNotifier | ( | const PNotifier & | func | ) |

Set the call back function that is called whenever the timer expires.

This is called by the `OnTimeout()` function.

void PTimer::Stop | ( | bool | wait = `true` | ) |

Stop a running timer.

The timer will not call the notification function and is reset back to the original timer value. Thus when the timer is restarted it begins again from the beginning.

The wait flag indicates that the function should wait for the timeout callback to complete before returning. That way external logic can be assured there is no race condition. However, under some circumstances this can cause a deadlock if the timeout function tries to acquire a mutex the calling thread already has, so an aysnchronouse version is provided. It is then the responsibility of the caller to handle the race condition with the timeout function.

static PTimeInterval PTimer::Tick | ( |  | ) | `[static]`

Get the number of milliseconds since some arbtrary point in time.

This is a platform dependent function that yields a real time counter.

Note that even though this function returns milliseconds, the value may jump in minimum quanta according the platforms timer system, eg under MS-DOS and MS-Windows the values jump by 55 every 55 milliseconds. The `Resolution()` function may be used to determine what the minimum time interval is.

Returns:
  millisecond counter.

The documentation for this class was generated from the following file:

- timer.h