A Timer Application Based on IReferenceClock | CodeGuru

A Timer Application Based on IReferenceClock

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 5, 2004
2 minute read
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.

Advertisement

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”.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.