Showing Changing Value in a Window Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Showing Changing Value in a Window

31 Jul 2021, 09:46

The value in the following window does not update every second. What has to be changed in the script, and is there a better way to display a changing value every second?

Code: Select all

seconds:=60

gui timer:new
gui timer:color, white
Gui timer:+AlwaysOnTop
Gui timer:Font, s50

SetTimer, timer, 1000
return

timer:
seconds:=seconds-1
gui timer:add, text, cGreen, %seconds%
gui timer:show
return
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Showing Changing Value in a Window

31 Jul 2021, 10:06

Code: Select all

seconds := 60
Gui, timer:New, +AlwaysOnTop
Gui, Color, White
Gui, Font, s50
Gui, Add, Text, cGreen vtime Center, %seconds%
Gui, Show,, Time
SetTimer, Timer, 1000
Timer:
SoundBeep, 1700
GuiControl, timer:, time, %seconds%
seconds -= 1
Return

GuiEscape:
GuiClose:
ExitApp
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Showing Changing Value in a Window

31 Jul 2021, 12:26

Thank you. This timer works.
Since the sound beep has a duration of more than 100 milliseconds, the countdown of seconds may not be precise. (The total time between each iteration of the timer is 1 second + the sound beep duration.)
Is there a way to make the countdown precise and independent of the delay caused by the sound beep?
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Showing Changing Value in a Window

31 Jul 2021, 12:34

Timing is not precise anyway, but if you want to retain the beep, you can simply subtract its duration from the timer frequency. The default beep duration is 150 ms.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Showing Changing Value in a Window

31 Jul 2021, 12:45

Timing is not precise anyway
I am planning to create a timer with the countdown for several hours. So if the timing of seconds is not precise even without the beep, the total divergence from the actual time may be big if the countdown timer is run for a long time.
Is it possible to make the countdown accurate and dependent on the system’s internal clock?
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Showing Changing Value in a Window

31 Jul 2021, 13:03

Yes, more or less. Instead of a timer, you can use Sleep and A_TickCount to make adjustments along the way. With each iteration, you would adjust the sleep to correspond to the delay needed to get to the next target interval. Most commands execute quickly and so would not need this sort of approach, but that depends on your specific script and what you need the script to do. If your script is meant to run every few hours, you can also simply use the Windows Task Scheduler to accomplish that.
Last edited by mikeyww on 31 Jul 2021, 13:06, edited 1 time in total.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Showing Changing Value in a Window

31 Jul 2021, 13:05

Alexander2 wrote:
31 Jul 2021, 12:26
Is there a way to make the countdown precise and independent of the delay caused by the sound beep?
This should be about +/- 0.20 seconds off real time (for the whole run)

Code: Select all

seconds := 60
remain := 0
Gui, timer:New, +AlwaysOnTop
Gui, Color, White
Gui, Font, s50
Gui, Add, Text, cGreen vtime Center, %seconds%
Gui, Show,, Time
SetTimer, Timer, 200
StartTime := A_TickCount
Timer:
if( ( A_TickCount - ( StartTime - remain ) ) > 1000 ) {
	remain := ( A_TickCount - StartTime ) - 1000
	StartTime := A_TickCount
	SoundBeep, 1700
	seconds -= 1
	GuiControl, timer:, time, %seconds%
	if(!Seconds)
		SetTimer, Timer, off
}
Return

GuiEscape:
GuiClose:
ExitApp
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Showing Changing Value in a Window

31 Jul 2021, 13:31

That last one would likely start to drift over time (hours).
This one should keep perfect time ( +/- 200 ms ) for unlimited duration.

Code: Select all

seconds := 60
Gui, timer:New, +AlwaysOnTop
Gui, Color, White
Gui, Font, s50
Gui, Add, Text, cGreen vtime Center, %seconds%
Gui, Show,, Time
SetTimer, Timer, 200
TargetTime := A_TickCount + ( seconds * 1000 )
Timer:
if( TargetTime - A_TickCount < ( seconds - 1 ) * 1000 ) {
	SoundBeep, 1700
	seconds -= 1
	GuiControl, timer:, time, %seconds%
	if(!Seconds)
		SetTimer, Timer, off
}
Return

GuiEscape:
GuiClose:
ExitApp
Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Showing Changing Value in a Window  Topic is solved

01 Aug 2021, 03:43

Hallo,
that one should keep perfect time ( +/- 20 ms ) for unlimited duration:

Code: Select all

seconds := 60
TickCount := A_TickCount
Gui, timer:New, +AlwaysOnTop
Gui, Color, White
Gui, Font, s50
Gui, Add, Text, cGreen vtime Center, %seconds%
Gui, Show,, Time
Timer:
GuiControl, timer:, time,% seconds
if(seconds--)
	SetTimer, Timer,% A_TickCount - TickCount += 1000
SoundBeep, 1700
Return

timerGuiEscape:
timerGuiClose:
ExitApp
Edit: As Hellbend correctly pointed out, my previous revision was wrong. I had inserted the script incompletely.
Last edited by Rohwedder on 02 Aug 2021, 01:12, edited 1 time in total.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Showing Changing Value in a Window

01 Aug 2021, 12:43

Thank you for the scripts. It will take some time for me to understand them.
I can see now how the principle works. It is necessary to check the current value against the A_TickCount value during every iteration and then to make up for any differences in order to ensure the correct count of time.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Showing Changing Value in a Window

01 Aug 2021, 16:18

@Rohwedder
It looks like you edited your code and it no longer functions.
Alexander2 wrote:
01 Aug 2021, 12:43
I can see now how the principle works. It is necessary to check the current value against the A_TickCount value during every iteration and then to make up for any differences in order to ensure the correct count of time.
Yeah, the timer is just used to check in with the real time and update things if needed.
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Showing Changing Value in a Window

02 Aug 2021, 11:55

Rohwedder wrote:
01 Aug 2021, 03:43
Hallo,
that one should keep perfect time ( +/- 20 ms ) for unlimited duration:

Code: Select all

seconds := 60
TickCount := A_TickCount
Gui, timer:New, +AlwaysOnTop
Gui, Color, White
Gui, Font, s50
Gui, Add, Text, cGreen vtime Center, %seconds%
Gui, Show,, Time
Timer:
GuiControl, timer:, time,% seconds
if(seconds--)
	SetTimer, Timer,% A_TickCount - TickCount += 1000
SoundBeep, 1700
Return

timerGuiEscape:
timerGuiClose:
ExitApp
Edit: As Hellbend correctly pointed out, my previous revision was wrong. I had inserted the script incompletely.
Thank you. I have tested the script with different SoundBeep delays (900, 500, and 10), and it is evident that it makes up for tick delays by automatically adjusting the tick delay value to match one second.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 194 guests