Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / Visual Basic
Article

Timers & Timings

Rate me:
Please Sign up or sign in to vote.
2.77/5 (7 votes)
7 Apr 20052 min read 63.3K   4.6K   24   7
Produce timed event flags, functions, threads & timings therof.

Introduction

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.

Background

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)
  {
    TIM_make_tmrF(5000-(TIM_ictr()%5000),&timer_id[2],
      one,(void*)(TIM_ictr()/1000),false);

    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],
      timer_id[1],timer_id[2],timer_id[3]);
  }
}
//
// 'thr' is an 18sec repeating thread, 
// spun by 'ticker-ticker' thread
//
void thr (void*prm) {printf("\nThread   thr  fires, ictr %5d, prm %d",
   TIM_ictr(),(long)prm);}
//
//
//
void main (int argc, char*argv[])
{
  HANDLE flg_han; unsigned long sts;
    
  TIM_make_tmr (20000,&timer_id[0],"",false);
  TIM_make_tmrF(13000,&timer_id[1],cbr,(void*)123,true);
  TIM_make_tmrF(11000,&timer_id[2],one,(void*)456,false);
  TIM_make_tmrT(18000,&timer_id[3],thr,(void*)789,true);

  flg_han = TIM_get_handle(timer_id[0]);

  printf("\nIDs %d,%d,%d,%d",timer_id[0],
      timer_id[1],timer_id[2],timer_id[3]);

  while(pM)
  {
    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();
  TIM_reset();
  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 ccrp.mvps.org).

History

  • 3.4 TIM_Src.cpp - Comment/Logging maintenance.

License

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


Written By
Web Developer
United Kingdom United Kingdom
BSc (St.Andrews(1963-67))
MSCE
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)

Comments and Discussions

 
GeneralSo many folders; so few files. Pin
WREY23-Apr-04 8:44
WREY23-Apr-04 8:44 
GeneralRe: So many folders; so few files. Pin
Lymington3-May-04 21:51
Lymington3-May-04 21:51 
Generalusing proprietary libraries Pin
Anthony_Yio1-Apr-04 14:28
Anthony_Yio1-Apr-04 14:28 
GeneralRe: using proprietary libraries Pin
Lymington1-Apr-04 20:57
Lymington1-Apr-04 20:57 
What I would really like is an accurate (software) interrupt every micro-second or better. In these days of giga-Hz processors what I am using is definitely out-of-date. Help?


Lymington
GeneralRe: using proprietary libraries Pin
Nitron2-Apr-04 3:06
Nitron2-Apr-04 3:06 
GeneralRe: using proprietary libraries Pin
Anthony_Yio2-Apr-04 14:55
Anthony_Yio2-Apr-04 14:55 
GeneralRe: using proprietary libraries Pin
f29-Apr-05 7:33
f29-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.