Page 1 of 1

Showing Changing Value in a Window

Posted: 31 Jul 2021, 09:46
by Alexander2
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

Re: Showing Changing Value in a Window

Posted: 31 Jul 2021, 10:06
by mikeyww

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

Re: Showing Changing Value in a Window

Posted: 31 Jul 2021, 12:26
by Alexander2
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?

Re: Showing Changing Value in a Window

Posted: 31 Jul 2021, 12:34
by mikeyww
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.

Re: Showing Changing Value in a Window

Posted: 31 Jul 2021, 12:45
by Alexander2
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?

Re: Showing Changing Value in a Window

Posted: 31 Jul 2021, 13:03
by mikeyww
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.

Re: Showing Changing Value in a Window

Posted: 31 Jul 2021, 13:05
by Hellbent
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

Re: Showing Changing Value in a Window

Posted: 31 Jul 2021, 13:31
by Hellbent
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

Re: Showing Changing Value in a Window  Topic is solved

Posted: 01 Aug 2021, 03:43
by Rohwedder
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.

Re: Showing Changing Value in a Window

Posted: 01 Aug 2021, 12:43
by Alexander2
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.

Re: Showing Changing Value in a Window

Posted: 01 Aug 2021, 16:18
by Hellbent
@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.

Re: Showing Changing Value in a Window

Posted: 02 Aug 2021, 11:55
by Alexander2
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.