Solved

Timer for Borlandc C++

Posted on 1998-08-31
4
773 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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