Click here to Skip to main content
13,454,124 members (49,696 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 31 Mar 2004

Timers & Timings

, 7 Apr 2005
Rate this:
Please Sign up or sign in to vote.
Produce timed event flags, functions, threads & timings therof.


This DLL provides fifteen routines to satisfy application program requirements for timers and timed activities in a C/C++ and VB environment. The praxis is that 150 timer slots should be enough for any application. Each timer may be set up to raise an event-flag, execute a function (on the ticker thread), or spin off a thread. A timer is allocated a number (i.e., 1 to 150) on creation. Timers may be cancelled or modified by timer number.


At initialization, two threads are spun off. The ticker-ticker thread fires every millisecond, using winmm.dll, the Windows (R) multi-media library (this library contains calls that are accurate to one millisecond). Each execution of this thread decrements positive-value timers. When a timer reaches zero, its action (one of three) is performed and the timer expires (negative state). The ticker thread is used to execute functions-to-be-called when timers expire and to exit the two threads, ticker-ticker and ticker, when so required. This ticker thread could become overtaxed since it must finish one function before it can begin another. In such a case, use make_tmrT instead of make_tmrF. Initialization is done when the first call to any timer or timings routine is made.

Using the code

VC 6.0 projects: Place the TIMDLL.dll in a directory on your path variable. Add the library TIMDLL.lib to the project resources. Add the module TIMcalls.h to the project. Use the routines therein.

VB 6.0 projects: Register the TIMDLL.dll with regsvr32. Add the module TIMDLL.bas to the project. Use the public routines therein.

// Source to produce the above console screen
#include "TIMcalls.h"
#include "stdio.h"// for printf
static bool pM = true; long timer_id[]= {0,0,0,0};
// 'cbr' is a 13sec repeating function, called by the 'ticker' thread
void cbr (void*prm) {printf("\nFunction cbr  fires, 
  ictr %5d, prm %d",TIM_ictr(),(long)prm);}
// 'one' is a one-shot function, called by the 'ticker' thread, which
// calls itself on 5sec boundaries while TIM_ictr < 30 seconds
void one (void*prm)
  printf("\nFunction one  fires, ictr %5d, prm %d",TIM_ictr(),(long)prm);
  if (TIM_ictr() < 30000)

    if (timer_id[0] > 0 && TIM_mod_tmr(timer_id[0],
            20000-TIM_ictr()) != TIM_SUCCESS)
       timer_id[0] = 0;//make_tmr has expired, clear ID

    printf("\nIDs %d,%d,%d,%d",timer_id[0],
// 'thr' is an 18sec repeating thread, 
// spun by 'ticker-ticker' thread
void thr (void*prm) {printf("\nThread   thr  fires, ictr %5d, prm %d",
void main (int argc, char*argv[])
  HANDLE flg_han; unsigned long sts;
  TIM_make_tmr (20000,&timer_id[0],"",false);

  flg_han = TIM_get_handle(timer_id[0]);

  printf("\nIDs %d,%d,%d,%d",timer_id[0],

    Sleep(1000-(TIM_ictr()%1000));// next second boundary

    sts = WaitForSingleObject(flg_han,0);// check for flag set

    if (sts != WAIT_OBJECT_0)
    printf("\nFlag    not yet set, ictr %5d",TIM_ictr());
    else{printf("\nFlag            set, ictr %5d",TIM_ictr()); pM=false;}

  Sleep(57000-TIM_ictr());// 57second boundary

  printf("\nAbout to reset TIM sub-system");
  long ictr = TIM_ictr();
  printf("\n  ...TIM_reset, about to exit");

  Sleep(80000-ictr);// 80second boundary (i.e.23secs) to read the screen

Points of Interest

For C/C++ only users, a static library can be built using the workspace and the project files provided. Slots used by event-flag setting timers are not re-used until absolutely necessary, thus the handle is available and the flag state may be queried after expiry. The more accurate 2nd demo VB project uses ccrpTmr.dll. Register the ccrpTmr.dll with regsvr32, ("common controls replacement project" DLL, free from


  • 3.4 TIM_Src.cpp - Comment/Logging maintenance.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United Kingdom United Kingdom
BSc (St.Andrews(1963-67))
Systems Programmer 39+yrs
Married to first wife 35yrs & counting, four grown-up children
Religious opinions similar to MelG's
It is not the gnosis, but the praxis must be the fruit. (Aristotle)

You may also be interested in...

Comments and Discussions

GeneralSo many folders; so few files. Pin
WREY23-Apr-04 8:44
memberWREY23-Apr-04 8:44 
GeneralRe: So many folders; so few files. Pin
Lymington3-May-04 21:51
memberLymington3-May-04 21:51 
Generalusing proprietary libraries Pin
Anthony_Yio1-Apr-04 14:28
memberAnthony_Yio1-Apr-04 14:28 
GeneralRe: using proprietary libraries Pin
Lymington1-Apr-04 20:57
memberLymington1-Apr-04 20:57 
GeneralRe: using proprietary libraries Pin
Nitron2-Apr-04 3:06
memberNitron2-Apr-04 3:06 
GeneralRe: using proprietary libraries Pin
Anthony_Yio2-Apr-04 14:55
memberAnthony_Yio2-Apr-04 14:55 
GeneralRe: using proprietary libraries Pin
f29-Apr-05 7:33
memberf29-Apr-05 7:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180321.1 | Last Updated 8 Apr 2005
Article Copyright 2004 by Lymington
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid