ATL Timer Component  28th June 2006

Associated Files: AppTimerBin.zip   AppTimer.zip

If you have a look on the internet, you will find there are literally dozens of code examples for Windows based timers. None of the examples I looked at exactly fulfilled my requirements. So, in time honoured fashion, I created my own timer component which, as it turns out, has proved to be one of my more useful pieces of software.

The ATL Timer is a non-visual component which can be used as a simple interval timer. It supports two modes of operation, one-shot and continuous. Typically, to use the component, you would first select the appropriate mode and timer timeout value. When ready, you would then start the timer and your application would receive the appropriate timeout notifications.

The component's main interface is very simple, having only six functions. There are start and stop functions, there are functions to set and retrieve the timeout value and there are also functions to set and retrieve the current timer mode. As mentioned above, there is a timer event interface, to which calling processes can connect and receive timeout notifications.

To receive these notification messages in your application, you should create a new class derived from MFC's CCmdTarget class, passing a timer pointer obtained from a call to CoCreateInstance as an argument. In this connection point class, you simply call AfxConnectionAdvise, passing the timer event interface ID to create the new connection point. An example of this function call can be seen below.

m_pUnkSink = this->GetIDispatch(FALSE);
AfxConnectionAdvise(m_pAppTimer, DIID__ITimerEvents, m_pUnkSink, TRUE, &m_dwCookie)

Note, you must remember to call AfxConnectionUnadvise in your class when the connection point is no longer required. A typical AfxConnectionUnadvise function call can be seen below.

AfxConnectionUnadvise(m_pAppTimer, DIID__ITimerEvents, m_pUnkSink, TRUE, m_dwCookie)

Having created a CCmdTarget class similar to that described above, you should now be able to start, stop and receive timer event notifications from within your application. Note, when using continuous mode with small timeout values, you should limit the amount of code executed when a timeout notification is received. Otherwise, the main application may not be able to execute all necessary code before the next timeout notification arrives.

The both source code and binary files for the ATL Timer component can be downloaded via the ayrware downloads page. Note, the component was created using Microsoft VC++ version 6.0.