home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / TIMER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-19  |  1.4 KB  |  51 lines

  1. /* TIMER.C - From page 511 of "Microsoft C Programming for      */
  2. /* the IBM" by Robert Lafore. Checks the initime() and timeout()*/
  3. /* functions from the same page.                                */
  4. /****************************************************************/
  5.  
  6. main()
  7. {
  8. long intval;
  9.  
  10.    while(1)  {
  11.       printf("\n\nEnter interval (hundreds of a second): ");
  12.       scanf("%D", &intval);
  13.       initime(intval);              /*initialize timer*/
  14.       printf("\n\nStart.\n");
  15.       while(!timeout())
  16.          ;
  17.       printf("\n\nEnd.\n");
  18.    }
  19. }
  20.  
  21. /* initime() */   /* initializes time delay for timeout() function */
  22. /* delays number of hundredths of a second in argument */
  23. /* increments every 5 or 6 hundredths sec */
  24.  
  25. #include <types.h>      /*defines time_b type in timeb*/
  26. #include <timeb.h>      /*defines time_b structure*/
  27. struct timeb xtime;     /*structure for time info*/
  28. long time1, time2;      /*start & running times*/
  29. long intval;            /*interval in hundredths*/
  30.  
  31. initime(hsecs)
  32. long hsecs;
  33. {
  34.  
  35.    intval = (hsecs < 12) ? 12 : hsecs;    /*minimum is 12*/
  36.    ftime(&xtime);
  37.    time1 = (long)xtime.millitm/10 + xtime.time*100;
  38. }
  39.  
  40. /* timeout() */   /* Returns 1 to function timer() */
  41. /* if time interval exceeded, 0 otherwise. */
  42. timeout()
  43. {
  44.  
  45.    ftime(&xtime);
  46.    time2 = (long)xtime.millitm/10 + xtime.time*100;
  47.    return((time2-time1 > intval) ? 1 : 0);
  48. }
  49.  
  50.  
  51.