BSL430.NET  1.2.1
Public Member Functions | Properties | List of all members
RJCP.Datastructures.TimerExpiry Class Reference

A class to maintain how much time is remaining since the last reset, until expiry. More...

Public Member Functions

 TimerExpiry (int milliseconds)
 Constructor. Initialise expiry based on the current time. More...
 
int RemainingTime ()
 Estimate the amount of time (ms) remaining from when this function is called until expiry. More...
 
void Reset ()
 Reset the time out so it occurs with the given Timeout. More...
 

Properties

int Timeout [get, set]
 The time for expiry on the next reset. System.Threading.Timeout.Infinite indicates no expiry. More...
 
bool Expired [get]
 Test if the timer expiry has expired. More...
 

Detailed Description

A class to maintain how much time is remaining since the last reset, until expiry.

This class is useful when implementing time outs in other methods. It can provide the remaining time, in units of milliseconds, that can be used with many Operating System calls as an expiry time.

One example is the M:System.Threading.WaitHandle.WaitOne method which expects a time out parameter. Either instantiate the TimerExpiry class at the beginning immediately before its use, or call the Reset method at the beginning of the time out operation. Then on return of the function, if no other operation occurred, the method RemainingTime should return 0 indicating that the timer has expired.

Another thread can be programmed to Reset the timer class during a time out operation, so that even if the result of Wait operation by the Operating system resulted in a time out, a Reset, which results in the RemainingTime being more than 0 milliseconds, indicates that another wait operation should occur.

Even if no expiry is to occur, but the Operating System function returns early, you can opt to restart the time out operation which will then take into account the current time and reduce the time out so that the operation ends as expected.

As an example, say you need to wait for data by calling a method which waits for the first set of data within a time out. But your method must wait for at least two elements of data within the time out. This can be implemented as follows:

public true MyFunc(int timeOut) { TimerExpiry myExpiry = new TimerExpiry(timeOut); int elements = 0; do { elements += GetData(myExpiry.RemainingTime()); } while (elements < 2 && !myExpiry.Expired);

if (elements >=2) return true; return false; }

Constructor & Destructor Documentation

◆ TimerExpiry()

RJCP.Datastructures.TimerExpiry.TimerExpiry ( int  milliseconds)

Constructor. Initialise expiry based on the current time.

Parameters
millisecondsThe initial time out in milliseconds.

The constructor sets the initial time out that should be used. On construction of the new object, the timer is automatically started.

Member Function Documentation

◆ RemainingTime()

int RJCP.Datastructures.TimerExpiry.RemainingTime ( )

Estimate the amount of time (ms) remaining from when this function is called until expiry.

Returns
The time to expiry in milliseconds.

◆ Reset()

void RJCP.Datastructures.TimerExpiry.Reset ( )

Reset the time out so it occurs with the given Timeout.

Property Documentation

◆ Expired

bool RJCP.Datastructures.TimerExpiry.Expired
get

Test if the timer expiry has expired.

◆ Timeout

int RJCP.Datastructures.TimerExpiry.Timeout
getset

The time for expiry on the next reset. System.Threading.Timeout.Infinite indicates no expiry.


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