Solved

Timer for Borlandc C++

Posted on 1998-08-31
4
771 Views
Last Modified: 2010-04-02
Hi everyone, I am using Borland C++ version 3.1 and I need to implement a timer that interrupts the program execution at defined intervals. The only timer commands in Borland C++ is a stopwatch and a command to fetch the system time. Does anyone have any ideas of getting a timer running to interrupt my program that will be doing lots of other things? Thanks
0
Comment
Question by:rose337
  • 3
4 Comments
 
LVL 2

Expert Comment

by:warmcat
ID: 1171651
Rosie,

If yours is a Windows app, you can use ::SetTimer() and ::KillTimer() (Win16 and Win32 API calls) to request that a window be sent periodic WM_TIMER messages.

If it's a DOS app, you can hook the system 18Hz 'clock tick' interrupt 0008.

If it's a console app, then the 'multimedia timer' is your man: look in the docs for ::timeSetEvent().  It does not need a window handle to do the deed, and takes a pointer callback function which is executed periodically.

An alternative methodology, if your program has an ''outer loop'' of some sort, is to check ::GetTickCount() at the top of your loop, and execute your periodic code after more than a certain number of ticks (==mS) have elapsed.

Regards,

-Andy
0
 

Author Comment

by:rose337
ID: 1171652
Thanks Andy, but that would work only for Visual C++. I am now in Borland C++ version 3.1. This is a dos based compiler and it is very old. The time functions are different as described above.
0
 
LVL 2

Accepted Solution

by:
warmcat earned 100 total points
ID: 1171653
> If it's a DOS app, you can hook the system 18Hz
> 'clock tick' interrupt 0008.

This is nothing to do with Windows: it is a periodic interrupt that goes all the way back to the very first PC.  It occurs at 18.2Hz, and you can hook it at 0000:0020...0000:0023.

Be sure to use a _asm CLI instruction before fiddling with it, and a _asm STI instruction after you have hooked it.  The contents of 0000:0020 is a far * to the interrupt routine, in (low-offset)(high-offset)(low-seg)(high-seg) form.

Your interrupt routine should JMP to the original address that was at 0000:0020 after doing its stuff.

Be aware that while in an Interrupt Service Routine, it is quite dodgy to use any DOS functions, as DOS was not designed to be reentrant.

-Andy
0
 
LVL 2

Expert Comment

by:warmcat
ID: 1171654
BTW, 0000:046C contains a 32-bit count of the clock ticks, maintained by DOS.  So, for example:

unsigned long RoseGet18HzTickCount()
{
   unsigned long * pul=(unsigned long *)0x0000046c;
   return (*pul);
}

would allow you to use the polling technique I initially outlined with 0.055S resolution.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now