A Threaded Timer Class

Environment: VC6 NT/98

A short while ago I was involved in an MFC project that was doing some simple packet routing. Some packets arrive in fragments, and at times fragments were lost, so there was a need to be able to expire uncompleted packets from the buffer.

It is for this reason that I developed this Timer class. It is an extremely simple class that invokes an operation given by the developer, every certain amount of seconds.

To use this class, you simply have to derive (or multiply inherit) the class that needs this timeout service from Timer. Then, you override the Tick() method and place in the overriden method the code that does whatever needs to be done when timeout occures. In my case this was an expiration of a packet from a buffer.

WHY NOT USE A WINDOWS TIMER? Well it is true that a windows timer ( CWnd::SetTimer() ), which invokes the WM_TIMER message, behaves in a similar way and does not require another thread. Yet, the timer messages get queued with other windows messages and at times, if the user interface is busy, the OnTimer() will not be invoked when it is needed but wait until all the other windows messages get handled. In addition to that, if there is complex processing to be done on timeout, the user interface would be unresponsive, if OnTimer() is used.

So if you need a sure way to get the timeout when you want it, regardless the state of the UI, and dont mind the overhead of another thread, this class is for you.

Well, this class is so simple that it is a waste of time to write about it anymore... Here is the complete implementation so you can see how it works:

// Timer.h: interface for the Timer class.

#if !defined(AFX_TIMELIMIT_H__D2107E6A_33E5_11D3_8D14_00E0980636C2__INCLUDED_)
#define AFX_TIMELIMIT_H__D2107E6A_33E5_11D3_8D14_00E0980636C2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Timer  
	void StopTicking();
	void StartTicking();
	virtual ~Timer();
	int GetTimeout(){return m_msTimeout;}
	void SetTimeout(int t){m_msTimeout=t;}
	int m_msTimeout;
	virtual void Tick();
	HANDLE m_hThreadDone;
	bool m_bStop;
	static UINT TickerThread(LPVOID pParam);

#endif // !defined(AFX_TIMELIMIT_H__D2107E6A_33E5_11D3_8D14_00E0980636C2__INCLUDED_)

// Timer.cpp: implementation of the Timer class.

#include "stdafx.h"
#include "Timer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW

// Construction/Destruction

	m_hThreadDone = NULL;
	m_hThreadDone = CreateEvent(NULL,FALSE, FALSE, NULL);
	ASSERT (m_hThreadDone);


	//dont destruct until the thread is done
	DWORD ret=WaitForSingleObject(m_hThreadDone,INFINITE);

void Timer::Tick()
	//Will be overriden by subclass


void Timer::StartTicking()
	if (m_bStop==false)
		return; ///ignore, it is already ticking...
	AfxBeginThread(TickerThread, this);

UINT Timer::TickerThread(LPVOID pParam)
	Timer* me=(Timer*) pParam;
	ASSERT (me->m_msTimeout!=-1);
	while (!me->m_bStop)
		Sleep (me->GetTimeout());
	return 0;

void Timer::StopTicking()
	if (m_bStop==true)
		return; ///ignore, it is not ticking...

	m_bStop=true; //ok make it stop
	//The above ensures that we do not return UNTIL the thread
	//has finished. This way we dont allow the user to start multiple
	//threads that will execute Tick() at the same time


To use it, call the SetTimeout() and then StartTicking() in the subclass. Make sure the subclass has a void Tick() method which includes the processing to be done at timeout. The demo project shows a CStatic derivative (CMyStatic) which also derives from Timer and on every tick, inverts its colors.


Download demo project - 37 Kb
Download source - 2 Kb