A Timer Application Based on IReferenceClock

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: Win 95 or later, Win 2000

Introduction

To program a MIDI-file player, I need a Windows timer with the best possible timekeeping capability because I send all MIDI events one after another. I have tried the Windows Multimedia Timer but I was not happy with it. Now I use the Reference Clock Interface of the class CBaseReferenceClock with less time latency.

The included project demonstrates how to program the interface of IReferenceClock. The MFC demo-program is a benchmark to get an idea of the exactness of the timer.

The clock counts with 100-nanosecond-entities but a maximal time-delay is not guaranteed. The sample is restricted with a resolution of 1 ms ( = 10000 ns) only.

Method

The underlined keywords of the following text are explained in the MSDN documentation.

The CBaseReferenceClock class provides an interface to the timer-functions of IReferenceClock. In the sample, I have applied the functions GetTime and the one-shot timer AdviseTime. The application is advised by the signal of an event. I have used the class CAMEvent. Within this class I choose the function Wait to wait for a signaled state of the event. GetTime delivers the time in the type REFERENCE_TIME. To check the exactness of the clock, I convert the difference between the times before and after the waiting period using the function ConvertToMilliseconds.

The timer function runs in its own thread with high priority using the functions CreateThread and SetThreadPriority. The user is able to interrupt the process through button-clicks. This is possible thanks to a loop with WaitForSingleObject-calls with a time-out interval of 1000 ms which uses PeekMessage and PumpMessage to look for and process incoming messages.

Programming Hints

The header file “Streams.h” must be included.

With the release version, the libraries winmm.lib, strmbase.lib and strmiids.lib are required.

With the debug version, the libraries winmm.lib, strmbasd.lib, and strmiids.lib are used. You will find strmbasd.lib only after compiling “DXSDK/Samples/C++/DirectShow/dshow.dsw” in “DXSDK/Samples/C++/DirectShow/BaseClasses/Debug_Unicode”.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read