Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSo many folders; so few files.
WREY
9:44 23 Apr '04  
You made this unnecessarily difficult to work with. A lot of these folders could have been avoided, resulting in a simpler sample with which to work. I still cannot figure out the rationale for so many folders.

Poke tongue

William

Fortes in fide et opere!
GeneralRe: So many folders; so few files.
Lymington
22:51 3 May '04  
Sorry about the extra folders. Did not notice them using usual zipper but found them using zipCentral. Have deleted spurious tree & updated.

Lymington
Generalusing proprietary libraries
Anthony_Yio
15:28 1 Apr '04  
using winmm.dll, the Windows(R) multi-media library is really a setback of this class.

Sonork 100.41263:Anthony_Yio

Life is about experiencing ...

GeneralRe: using proprietary libraries
Lymington
21:57 1 Apr '04  
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
Nitron
4:06 2 Apr '04  
Lymington wrote: 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?
Use IRIX or VX-Works, or some other RTOS. Windows isn't, shouldn't be, and never will be an RTOS. They are different operating paradigms best suited to their own respective environments. Windows is event-driven because the humans that use it are event-driven. You shouldn't look to a windows platform for RT applications any more than you would look to an RTOS for database or word-processing applications.

~Nitron.
ññòòïðïðB A
start

GeneralRe: using proprietary libraries
Anthony_Yio
15:55 2 Apr '04  
Have you tried the QueryPerformanceFrequency or QueryPerformanceCounter API to implement your function? or Timer.h or boost::timer;(for sticking to pure C++ by not having to be dependent to other proprietary libraries)

Sonork 100.41263:Anthony_Yio

Life is about experiencing ...

GeneralRe: using proprietary libraries
f2
8:33 9 Apr '05  
i heard that Zen-Timer is the best. keyword search michael abrash graphics programming black book.

from,
-= aLbert =-


Last Updated 8 Apr 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010