Sign in to follow this  
Followers 0
Rapid

Timer Problems

11 posts in this topic

#1 ·  Posted (edited)

 

$Form1 = GUICreate("D2", 128, 77, 292, 327)
$Timer = GUICtrlCreateInput("0", 2, 3, 25, 21)
Func Timer($val)
Dim $i_TimeStamp = TimerInit()
Dim $i_TimeToCountDownFromSeconds = $val
Dim $i_OriginalSeconds = $i_TimeToCountDownFromSeconds
    While $i_TimeToCountDownFromSeconds > 0
    If TimerDiff($i_TimeStamp) >= 100 Then
        $i_TimeStamp = TimerInit()
        IF $i_TimeToCountDownFromSeconds = 0 Then Exitloop
        $i_TimeToCountDownFromSeconds -= 0.1
        GUICtrlSetData($Timer, $i_TimeToCountDownFromSeconds)
          IF GUICtrlRead($Timer) < 0 then
              GUICtrlSetData($Timer,"0")
        EndIf
    EndIf
    sleep(10)
    WEnd
EndFunc
[/code]

 

For some reason when I use this, and for example if I set the timer val to be 3 (3 seconds) with jumps of 0.1 milisecond

untill 1 its all fine, but when it goes below 1 to 0.9 it shows a random number (example: 90349030) untill it reaches 0

I dont not want to miss the 0.9 to 0 process.

Any code fix you guys could suggest?

$val stands for the time to count down from

 

 

 

And another question - 

How can I make 2 timers run at the same time? I cant get it to work :(. When I start 1 timer and then the 2nd timer it will just pause the first one and start the second one

 

Edited by Rapid

Share this post


Link to post
Share on other sites



Try this...

GUICtrlSetData($Timer, stringformat('%2.1f',$i_TimeToCountDownFromSeconds))

 


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

JohnOne ofocurse it doesnt run.. I only copied a few lines q_q.

And I still have problem... they wont run at the same time !!! 

If I turn one of them on the 2nd one will stop.... what can I do in order to make them all run at the same time if I want to... 

if  I have 1 timer runnign from 10 to 0 and its on 5 at the moment and I start the second timer then it will run 10 to 0 on the second timer and only then keep going with the first one... why is that?? any help :(?

Share this post


Link to post
Share on other sites

#include <AVIConstants.au3>
#include <GuiConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <GUIConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#Region ### Global Vars ###
Global Const $sLogFile = 'log.txt'
Global $hFileOpen = FileOpen($sLogFile, $FO_APPEND)
#EndRegion ### Global Vars ###
#Region ### HotKeys ###
HotKeySet("{ESC}", "ExitProg")
HotKeySet("^z","S1")
HotKeySet("^x","S2")
HotKeySet("^c","S3")
HotKeySet("^v","S4")
#EndRegion ### HotKeys ###
#Region ### GUI ###
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("D2", 128, 77, 292, 327)
$Input1 = GUICtrlCreateInput("Txt", 2, 3, 25, 21)
$Input2 = GUICtrlCreateInput("Txt", 34, 3, 25, 21)
$Input3 = GUICtrlCreateInput("Txt", 66, 3, 25, 21)
$Input4 = GUICtrlCreateInput("Txt", 98, 3, 25, 21)
$S1T = GUICtrlCreateInput("", 2, 29, 26, 21)
$S2T = GUICtrlCreateInput("", 34, 29, 26, 21)
$S3T = GUICtrlCreateInput("", 66, 29, 26, 21)
$S4T = GUICtrlCreateInput("", 98, 29, 26, 21)
$Timer1 = GUICtrlCreateInput("0", 2, 53, 26, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY))
$Timer2 = GUICtrlCreateInput("0", 34, 53, 26, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY))
$Timer3 = GUICtrlCreateInput("0", 66, 53, 26, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY))
$Timer4 = GUICtrlCreateInput("0", 98, 53, 26, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd
#EndRegion ### GUI ###

func S1()
$time = GUICtrlRead($S1T)
Timer($time,$Timer1)
EndFunc
func S2()
$time = GUICtrlRead($S2T)
Timer2($time,$Timer2)
EndFunc
func S3()
$time = GUICtrlRead($S3T)
Timer($time,$Timer3)
EndFunc
func S4()
$time = GUICtrlRead($S4T)
Timer($time,$Timer4)
EndFunc
 
Func Timer($val,$Timer)
Dim $i_TimeStamp = TimerInit()
Dim $i_TimeToCountDownFromSeconds = $val
Dim $i_OriginalSeconds = $i_TimeToCountDownFromSeconds
    While $i_TimeToCountDownFromSeconds > 0
    If TimerDiff($i_TimeStamp) >= 100 Then
        $i_TimeStamp = TimerInit()
        IF $i_TimeToCountDownFromSeconds = 0 Then Exitloop
        $i_TimeToCountDownFromSeconds -= 0.1
        GUICtrlSetData($Timer1, stringformat('%2.1f',$i_TimeToCountDownFromSeconds))
          IF GUICtrlRead($Timer) < 0 then
              GUICtrlSetData($Timer,"0")
        EndIf
    EndIf
    sleep(10)
    WEnd
EndFunc
Func Timer2($val,$Timer)
Dim $i_TimeStamp = TimerInit()
Dim $i_TimeToCountDownFromSeconds = $val
Dim $i_OriginalSeconds = $i_TimeToCountDownFromSeconds
    While $i_TimeToCountDownFromSeconds > 0
    If TimerDiff($i_TimeStamp) >= 100 Then
        $i_TimeStamp = TimerInit()
        IF $i_TimeToCountDownFromSeconds = 0 Then Exitloop
        $i_TimeToCountDownFromSeconds -= 0.1
        GUICtrlSetData($Timer2, stringformat('%2.1f',$i_TimeToCountDownFromSeconds))
          IF GUICtrlRead($Timer2) < 0 then
              GUICtrlSetData($Timer2,"0")
        EndIf
    EndIf
    sleep(10)
    WEnd
EndFunc

while(1) ;always active
    sleep(100)
WEnd
Func ExitProg()
    Exit
EndFunc   ;==>ExitProg

 

just set values in the second line (numbers)

and hit ctrl+z and ctrl+x ... you will see what happen and maybe help me solve it :(

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

$i_TimeStamp  is the only timer you are using.  Therfore your 2ND call just resets the timer to whatever is current when you call it.  You need different variables for different timers.  Arrays are really handy for this.

kylomas

Edited by kylomas
spelling

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Stripped your code down to show basic example of showing two counters.

Just try to get what and why, then you can adapt principal to your goal.

#include <AVIConstants.au3>
#include <GuiConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <GUIConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#Region ### Global Vars ###
Global Const $sLogFile = 'log.txt'
Global $bTimer1Started = False, $bTimer2Started = False, $time1, $time2, $FuncStarted = False
Global $hFileOpen = FileOpen($sLogFile, $FO_APPEND)
#EndRegion ### Global Vars ###
#Region ### HotKeys ###
HotKeySet("{ESC}", "ExitProg")
HotKeySet("^1", "S1")
HotKeySet("^2", "S2")

#EndRegion ### HotKeys ###
#Region ### GUI ###
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("D2", 228, 177, 292, 327)
$Input1 = GUICtrlCreateInput("Txt", 2, 3, 100, 21)
$Input2 = GUICtrlCreateInput("Txt", 120, 3, 100, 21)
$Abort = GUICtrlCreateButton("Abort", 10, 100)

$S1T = GUICtrlCreateInput("", 2, 29, 100, 21)
$S2T = GUICtrlCreateInput("", 120, 29, 100, 21)

$hTimer1 = GUICtrlCreateInput("0", 2, 53, 100, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY))
$hTimer2 = GUICtrlCreateInput("0", 120, 53, 100, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY))

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
#EndRegion ### GUI ###

Func S1()
    If $bTimer1Started Then Return
    $time1 = TimerInit()
    $bTimer1Started = True
    If $FuncStarted Then Return
    Timer()
EndFunc   ;==>S1
Func S2()
    If $bTimer2Started Then Return
    $bTimer2Started = True
    $time2 = TimerInit()
    If $FuncStarted Then Return
    Timer()
EndFunc   ;==>S2

Func Timer()
    $FuncStarted = True
    While 1
        If $bTimer1Started Then
            GUICtrlSetData($hTimer1, Round(TimerDiff($time1), 2))
        EndIf
        If $bTimer2Started Then
            GUICtrlSetData($hTimer2, Round(TimerDiff($time2), 2))
        EndIf
        Sleep(500)
        If GUIGetMsg() = -3 Then Exit
        If GUIGetMsg() = $Abort Then ExitLoop
    WEnd
    $FuncStarted = False
    $bTimer1Started = False
    GUICtrlSetData($hTimer1, "")
    $bTimer2Started = False
    GUICtrlSetData($hTimer2, "")
EndFunc   ;==>Timer

Func ExitProg()
    Exit
EndFunc   ;==>ExitProg

 

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

thanks but I cant manage to make it count down... :\ and in 2.1f format

Share this post


Link to post
Share on other sites

And I tried doing it the same way you did and it didn't work :(.

Ill post the code when I wake up its on my laptop and see maybe u can understand why, thanks !

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0