Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / Visual Basic

Timers & Timings

Rate me:
Please Sign up or sign in to vote.
2.77/5 (7 votes)
7 Apr 20052 min read 63.6K   4.6K   24  
Produce timed event flags, functions, threads & timings therof.
// ============================================================================	//
//			    T i m _ t s t . c p p				//
// ============================================================================	//
#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		//
// ============================================================================	//
static 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			//
// ============================================================================	//
static 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		//
// ============================================================================	//
static 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
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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