Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Using Timer in Widows Programs.  (Read 9495 times)

0 Members and 1 Guest are viewing this topic.

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Using Timer in Widows Programs.
« on: June 01, 2017, 07:47:20 PM »
Somebody asked about this a did not get a full answer. Window and Linux both have some way to measure how much time has elapsed without the need to create a makeshift timing loop.
Doing direct machine level reset of any system timer is not a good idea. Instead, use the functions available in the system.

Here are some useful references:

http://www.wikihow.com/Add-a-Timer-in-Visual-Basic
How to Add a Timer in Visual Basic
One of the processes you should learn as a beginner in Visual Basic is how to add a timer. A timer can be useful when creating games, quizzes, or to limit the time a certain page is viewed. Here are some simple steps in how to add a timer to your Visual Basic application.

Programming a Timer in C++
http://www.cplusplus.com/forum/beginner/76147/

This timer can be used in VBA in a spreadsheet.
https://msdn.microsoft.com/en-us/library/office/gg264416.aspx

More involved:
https://www.daniweb.com/programming/software-development/code/459722/a-timer-for-vba-excel

About Timers
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644900(v=vs.85).aspx
This topic describes how to create, identify, set, and delete timers. An application uses a timer to schedule an event for a window after a specified time has elapsed. Each time the specified interval (or time-out value) for a timer elapses, the system notifies the window associated with the timer. Because a timer's accuracy depends on the system clock rate and how often the application retrieves messages from the message queue, the time-out value is only approximate.

None of this is new. Just want to let beginners knew about timers.    :)

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Using Timer in Widows Programs.
« Reply #1 on: June 02, 2017, 09:11:15 AM »
To add to this... in Batch Timer methods

Use of CHOICE instruction as a Timer ... https://www.computerhope.com/issues/ch001678.htm


SLEEP utility as a Timer ... https://www.computerhope.com/dutil.htm


PING 127.0.0.1 -n 15   (* Where you can PING your Home IP of a computer with an active network adapter for 15 times which takes around 15 seconds but not precise. This number can be any number for as many pings as you want to have running, but if for any reason the Ping fails, you can have the batch jump ahead to the next instruction if a system is lacking a Network Adapter etc. With just about all computers these days with active network adapters it works on most Windows systems. )


I use the SLEEP (or) PING method for my batches.  :)


camerongray



    Expert
  • Thanked: 306
    • Yes
    • Cameron Gray - The Random Rambings of a Computer Geek
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Mac OS
Re: Using Timer in Widows Programs.
« Reply #2 on: June 02, 2017, 04:12:20 PM »
Personally I'd avoid using ping for that sort of thing, seems like a massive hack, like using a screwdriver to hammer in a nail, sure it might work but you're using the wrong tool for the job.  If you absolutely have to use batch then I'd always recommend using one of the proper dedicated sleep methods rather than using another task that takes a relatively consistent time.  Nowadays if I have to do Windows scripting I'll almost always use Powershell, it has proper built in sleep method and has a syntax that I find remotely bearable.

As far as Geek's comment of "measure how much time has elapsed without the need to create a makeshift timing loop" goes, I'm interpreting this as you have an operation and you want to work out how long it would take.  Most decent datetime libraries have functionality to get time in terms of some epoch (seconds/milliseconds since a predefined date) for example on UNIX it is the number of seconds since 1970-01-01 at 00:00:00.  All you need to do is store the value of this before the operation you want to time starts then afterwards find the difference between the current epoch time and the time you had stored to see how much time has elapsed.

It's questions like this that really make me wonder why people seem to use batch files so much.  Sure they are fine for simple things like automating basic tasks but when you are needing features like decent time functionality then you are just making life much harder for yourself by using batch.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Using Timer in Widows Programs.
« Reply #3 on: June 02, 2017, 04:30:37 PM »
The Timers on the original post aren't for measuring elapsed time; they are for executing a callback at an approximate interval as described in the last link, quite a different thing.

For measuring elapsed time you would tend to prefer to use the System Performance Counter, rather than the system timer, though System Timers work well enough for a rough idea.

Most higher level languages have the structures/implementation already available. NET languages have System.Diagnostics.StopWatch specifically for measuring spans of time between segments of logic, for example.
I was trying to dereference Null Pointers before it was cool.

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Using Timer in Widows Programs.
« Reply #4 on: June 02, 2017, 06:50:36 PM »
Thanks for you posts.
Often batch file programmers want some way to either give an user some time to respond or otherwise a measure how long  some operation takes.
Look at this:
https://stackoverflow.com/questions/4313897/timer-in-windows-batch-file
It can take some timer to read over the comments the follow the above post about using a a time to measure elapsed time in batch.
It  take less time to use a tool rather that read the comments.
A pure "batch" method is not need.
Use the Time command in ms-dos to get the stat time. Memorize it, The use it again to get the stop time. Next do that calculations mentally.
Like this:
Code: [Select]
The current time is: 17:39:25.85Now start some batch file that takes some time to finish.
When it stops, get the time again.
Code: [Select]
The current time is: 17:53:11.00
Now just mentally do the subtraction
  fetch the stop time 17:53:11.00
  subtract start time 17:39:25.85

Most of you can do that in about 40 seconds.
It takes me twice the time at my age.  :-[

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Using Timer in Widows Programs.
« Reply #5 on: June 02, 2017, 07:38:53 PM »
Use the Time command in ms-dos to get the stat time. Memorize it, The use it again to get the stop time. Next do that calculations mentally.

In fact, why use the time command? Why not just use a clock?

Actually, why even use a clock? What you should do is wait for a sunny day, then you can mark out the shadow of an upright stick. After the thing you want to measure is finished, measure the shadow again, then determine the difference between the angle of the first shadow and the second. Then, you merely need to consider the Earth's rotational speed at your latitude and determine how much the Earth Rotated for the incident angle of the sun to change by that angle, and you can determine how much time passed. No need to overcomplicate it with things like "Commands".
I was trying to dereference Null Pointers before it was cool.

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Using Timer in Widows Programs.
« Reply #6 on: June 02, 2017, 08:24:51 PM »
....
Actually, why even use a clock? What you should do is wait for a sunny day, then you can mark out the shadow of an upright stick. After the thing you want to measure is finished, measure the shadow again ...
Yes, that is brilliant!   ;D

Here is a DIY project based on your suggestion.
http://www.anycalculator.com/horizontalsundial.htm


DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Using Timer in Widows Programs.
« Reply #7 on: June 03, 2017, 06:41:48 AM »
camerongray wrote:

Quote
Personally I'd avoid using ping for that sort of thing, seems like a massive hack, like using a screwdriver to hammer in a nail, sure it might work but you're using the wrong tool for the job.

 ;D Yes it is like using a screwdriver to hammer in a nail I suppose. Its like calling just about any other routine that would waste time doing something knowing it would take a certain period of time before it ended to step to the next batched instruction as a means of a delay timer.

 Not going to mention names, but I learned of this method here at CH some time ago. There was a system that didnt have SLEEP command that needed a time delay and SLEEP wasnt able to be installed to the system. It worked to get the job done given the restrictions, and creative method of achieving a delay timer.  :P

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Using Timer in Widows Programs.
« Reply #8 on: June 03, 2017, 10:29:52 AM »
DaveLembke,
Thanks for your post.
Now, to be serious. Here is what I recall.

The original design of the IBM PC has a real time clock that could give the time and date, if it had been set right. Later a hardware device with a tiny hearing aid cell was used so it would always have the right time.battery was
The system had a hardware interrupt that come on 18.2 times per second. This was about 55 milliseconds. It was drive by hardware independent of the CPU, so even if the CPU was inside a program loop, the interrupt would come and update the time.
Years ago I wrote some assembly programs to read the date and time functions from the system. I also use a BASIC program to control a serial port data feed used in a commercial communications system. Use of the timer in that program was very important. Some old serial port protocols depended on a good way to measure time in fractions of a second.
Anyway, all of  this is very old stuff. I comes to me that some will use  PING for a timer. The system already has  definitions of a hardware timers as part of the hardware and firmware. In fact, that is how PING works.

A micro timer was introduced in Turbo BASIC from Borland.
https://archive.org/details/bitsavers_borlandBorsHandbook1987_15768512
That was a long time ago.

My recommendation is that if you need a timer in batch, just invoke a bit of code in VBscript. Every Windows system has VBscript already installed.
https://en.wikipedia.org/wiki/VBScript

The intent here is to help newcomers with using time delays in programming. It is not necessary to re-invent the timer.   :)