FANDOM


GetTickCount is a Windows API function which retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

The elapsed time is a 32-bit number. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

It is easy to use GetTickCount incorrectly, and for this reason it is not recommended to be used directly in production code. The policy is to use two wrapper classes that encapsulate the typical functionality that GetTickCount is for, namely scheduling.

This page is an example of Catching defects with Patterns.

CodeEdit

//==============================================================================
// Application code should use CTickCounter and CTickTimer classes instead
// of directly calling the Windows API function GetTickCount.  The aim of these
// classes is to prevent bad usage of GetTickCount and avoid 49.7 day issues.
//
// Note that the "sizeof" each class is still 32-bits, so an existing DWORD data
// member in a struct or a class can be replaced with the class.
//
// Furthemore, all functions inline in release builds, so there's no performance
// hit when using these functions, over calling GetTickCount directly.
//
//
// Usage:
//
// Ex. 1 - Create a 5000 ms timer and determine when the timeout is reached
//
//     CTickTimer<5000> timer;
//     if ( timer.IsDue() )        // Calling IsDue() immediately after timer is created will return false.
//         ...;
//
//     Sleep( 5100 );              // Wait over 5000 ms, and try again.
//
//     if ( timer.IsDue() )        // Now IsDue() will return true.
//         ...;
//
//     timer.SetToCurrentTick();   // Reset the timer.  Now it's not due for another 5000 ms.
//
//
// Ex. 2 - Manually use the counter.  Useful when different time-outs are necessary against the same counter.
//
//     CTickCounter counter;
//     Sleep ( 1100 );                       // Wait over 1000 ms.
//     if ( counter.TicksSince() > 1000 )    // Test is true because it's been over 1000 ms since SetToCurrentTick() was last called.
//         ...;
//     if ( counter.TicksSince() > 5000 )    // Test is false because it hasn't been over 5000 ms since SetToCurrentTick() was last called.
//         ...;
//
//==============================================================================

class CTickCounter
{
protected:
	// Private so clients doesn't access to the tick-count value.
	int m_tickCount;

public:
	CTickCounter()
	{
		SetToCurrentTick();
	}

	// To set the tick to the current system tick-count
	void SetToCurrentTick()
	{
		m_tickCount = ::GetTickCount();
	}

	// Returns ticks that have passed since SetToCurrentTick() was last called.
	int TicksSince()
	{
		return ::GetTickCount() - m_tickCount;
	}

	// This method is only used for diagnostics, for displaying the number in the trace msgs, etc.
	// Do not use to get to the internal tick-count of this class.
	DWORD Diagnose()
	{
		return static_cast<DWORD>( m_tickCount );
	}
};


template <int elapsedTime>
class CTickTimer : public CTickCounter
{
public:
	// If current time past due 
	bool IsDue()
	{
		return TicksSince() >= elapsedTime;
	}
};

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.