SaeidN

Timer

21 posts in this topic

Hi,

I searched a lot here but didn't find what I want. Most of them were countdown and other stuff and back to 2004.

I just want a normal timer that when a button on gui is pressed, it starts from 00:00:00 and goes up.

Share this post


Link to post
Share on other sites



Should be pretty simple with a GUI and an AdlibRegister() function that calls a Func updating the Control in the GUI.
What do you have we can play with?

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
17 minutes ago, Jos said:

Should be pretty simple with a GUI and an AdlibRegister() function that calls a Func updating the Control in the GUI.
What do you have we can play with?

Jos

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 286, 140, 192, 124)
Global $StartBtn = GUICtrlCreateButton("Start", 32, 64, 99, 41)
Global $StopBtn = GUICtrlCreateButton("Stop", 168, 64, 91, 41)
Global $Label1 = GUICtrlCreateLabel("00:00:00", 104, 16, 82, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $StartBtn
            runfunction()
        Case $StopBtn
            
    EndSwitch
WEnd

Func runfunction()
    ;codes here
EndFunc

This is an example of my code. When I click on start btn, is calls a func. and at the same time I want the timer which is 00:00:00 to start.

And when I click on stop, I want both timer and function stop. (without closing gui)

Share this post


Link to post
Share on other sites

I don't see the  AdlibRegister()  function like I suggested so did you open the helpfile and checked it out?
Give it a try and post back what you tried.

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
25 minutes ago, Jos said:

I don't see the  AdlibRegister()  function like I suggested so did you open the helpfile and checked it out?
Give it a try and post back what you tried.

Jos

Thank you Jos.

I made this and it's working, but since in my function I have loops, so the stop button doesn't work. (I made that while loop a comment)

#include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 286, 140, 192, 124)
Global $StartBtn = GUICtrlCreateButton("Start", 32, 64, 99, 41)
Global $StopBtn = GUICtrlCreateButton("Stop", 168, 64, 91, 41)
Global $TimeLabel = GUICtrlCreateLabel("00:00:00", 104, 16, 82, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $StartBtn
            Example()
        Case $StopBtn
            AdlibUnRegister("MyAdLibFunc")
    EndSwitch
WEnd

Func Example()
    AdlibRegister("MyAdLibFunc",1000)
    ;While 1
        ;codes here
    ;WEnd
EndFunc

Func MyAdLibFunc()
    Local Static $iCount = 0
    $iCount += 1
    $sec2time_hour = Int($iCount / 3600)
    $sec2time_min = Int(($iCount - $sec2time_hour * 3600) / 60)
    $sec2time_sec = $iCount - $sec2time_hour * 3600 - $sec2time_min * 60
    $mtime = StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)
    _GUICtrlEdit_SetText($TimeLabel, $mtime)
EndFunc

 

Share this post


Link to post
Share on other sites

Anyone know how to get the stop button to work while it's doing the loop?

Thanks

Share this post


Link to post
Share on other sites
12 minutes ago, Jos said:

The stop button works fine with your code.... What is going wrong for you?

Jos

Uncomment the while loop which I have in my code, then stop button doesn't work.

Because I have loops in my func, so it won't work, and I don't know how to get it to work.

Share this post


Link to post
Share on other sites

That's clear ...  so why do you feel you need that loop there?
In case you do need a loop you simply convert the GUI Start/Stop buttons to use  "On Event" mode. See Helpfile for how that works

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

1 hour ago, Jos said:

That's clear ...  so why do you feel you need that loop there?
In case you do need a loop you simply convert the GUI Start/Stop buttons to use  "On Event" mode. See Helpfile for how that works

Jos

#include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 286, 140, 192, 124)
Global $StartBtn = GUICtrlCreateButton("Start", 32, 64, 99, 41)
GUICtrlSetOnEvent(-1, "Example")
Global $StopBtn = GUICtrlCreateButton("Stop", 168, 64, 91, 41)
GUICtrlSetOnEvent(-1, "_Pause")
Global $TimeLabel = GUICtrlCreateLabel("00:00:00", 104, 16, 82, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $StartBtn
            Example()
        Case $StopBtn
            _pause()
    EndSwitch
WEnd

Func _Pause()
    Local Static $bPaused = False
    If $bPaused = True Then
        AdlibRegister("MyAdLibFunc",1000)
        ConsoleWrite( "Unpaused")
    Else
        AdlibUnRegister("MyAdLibFunc")
        ConsoleWrite ("Paused")
    EndIf
    $bPaused = Not $bPaused
    While $bPaused
        Sleep(300)
    WEnd
EndFunc

Func Example()
    AdlibRegister("MyAdLibFunc",250)
    While 1
        ;codes here
    WEnd
EndFunc

Func MyAdLibFunc()
    Local Static $iCount = 0
    $iCount += 1
    $sec2time_hour = Int($iCount / 3600)
    $sec2time_min = Int(($iCount - $sec2time_hour * 3600) / 60)
    $sec2time_sec = $iCount - $sec2time_hour * 3600 - $sec2time_min * 60
    $mtime = StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)
    _GUICtrlEdit_SetText($TimeLabel, $mtime)
EndFunc

It's working partially. I wrote that pause func, so it's actually a pause and unpause button. But unpause doesn't work. In the help file it's saying " GUIGetMsg()" is not needed anymore, but I don't know how to remove it. Whole while loop? or just that or ...?

Edited by SaeidN

Share this post


Link to post
Share on other sites

#11 ·  Posted

The idea is that your main loop is now located under they GUI ..something like:

#include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)
Global $Running = 0

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 286, 140, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
Global $StartBtn = GUICtrlCreateButton("Start", 32, 64, 99, 41)
GUICtrlSetOnEvent(-1, "_Start")
Global $StopBtn = GUICtrlCreateButton("Stop", 168, 64, 91, 41)
GUICtrlSetOnEvent(-1, "_Stop")
Global $TimeLabel = GUICtrlCreateLabel("00:00:00", 104, 16, 82, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    If $Running Then
        ; code to perform when Started
    Else
        ; code to perform when Stopped
    EndIf
WEnd

Func _Stop()
    AdlibUnRegister("MyAdLibFunc")
    $Running = 0
    ConsoleWrite("Stopped")
EndFunc   ;==>_Stop

Func _Start()
    AdlibRegister("MyAdLibFunc", 250)
    $Running = 1
    ConsoleWrite("Started")
EndFunc   ;==>_Start

Func MyAdLibFunc()
    Local Static $iCount = 0
    $iCount += 1
    $sec2time_hour = Int($iCount / 3600)
    $sec2time_min = Int(($iCount - $sec2time_hour * 3600) / 60)
    $sec2time_sec = $iCount - $sec2time_hour * 3600 - $sec2time_min * 60
    $mtime = StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)
    _GUICtrlEdit_SetText($TimeLabel, $mtime)
EndFunc   ;==>MyAdLibFunc

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            Exit
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            MsgBox($MB_SYSTEMMODAL, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            MsgBox($MB_SYSTEMMODAL, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
    EndSelect
EndFunc   ;==>SpecialEvents

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#12 ·  Posted

3 hours ago, Jos said:

That's clear ...  so why do you feel you need that loop there?
In case you do need a loop you simply convert the GUI Start/Stop buttons to use  "On Event" mode. See Helpfile for how that works

Jos

#include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)
Global $Running = 0

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 286, 140, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
Global $StartBtn = GUICtrlCreateButton("Start", 32, 64, 99, 41)
GUICtrlSetOnEvent(-1, "_Start")
Global $StopBtn = GUICtrlCreateButton("Stop", 168, 64, 91, 41)
GUICtrlSetOnEvent(-1, "_Stop")
Global $TimeLabel = GUICtrlCreateLabel("00:00:00", 104, 16, 82, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    If $Running Then
        myfunc()
    Else
        Sleep(300)
    EndIf
WEnd

Func myfunc()
    $x=1
    While $x < 10
        ConsoleWrite("Running..." & @CR)
        $x+=1
    WEnd
EndFunc


Func _Stop()
    AdlibUnRegister("MyAdLibFunc")
    $Running = 0
    ConsoleWrite("Stopped")
EndFunc   ;==>_Stop

Func _Start()
    AdlibRegister("MyAdLibFunc", 250)
    $Running = 1
    ;myfunc()
    ConsoleWrite("Started")
EndFunc   ;==>_Start

Func MyAdLibFunc()
    Local Static $iCount = 0
    $iCount += 1
    $sec2time_hour = Int($iCount / 3600)
    $sec2time_min = Int(($iCount - $sec2time_hour * 3600) / 60)
    $sec2time_sec = $iCount - $sec2time_hour * 3600 - $sec2time_min * 60
    $mtime = StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)
    _GUICtrlEdit_SetText($TimeLabel, $mtime)
EndFunc   ;==>MyAdLibFunc

Func _exit()
    Exit
EndFunc

When I add my function which should have loop, the stop button doesn't work.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

5 minutes ago, Jos said:

Posted script works fine here. (Don't add MyFunc() to _Start() !)

Jos

Yeah, that was for test, then I comment it.

When I click on stop, the timer stops, but the console which is myfunc is running. I wans myfunc to pause. untill I press Start or stop button.

Edited by SaeidN

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

2 hours ago, Jos said:

Then simply add a test for 

If $Running Then

in your loop to determine whether Stop was pressed of not.

Jos

I put a consolewrite in if running. When I click start, it starts and never stops,

Then I put it in else, when i press f5, it starts and never stops again.

So it's going into the while loop, but only one of them, and doesn't finish. How can I stop it using buttons?

Edited by SaeidN

Share this post


Link to post
Share on other sites

#18 ·  Posted

19 minutes ago, Jos said:

Show your code or else it's a guessing game. ;)

Jos

Run the code in post 12. press start, time starts, consolewrite starts. So far it's ok.

Now click on stop, time stops which is ok, but the consolewrite doesn't stop. I want to have that loop, and stop it.

Share this post


Link to post
Share on other sites

#19 ·  Posted

So just do as I suggested !  something like this:

#include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)
Global $Running = 0

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 286, 140, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
Global $StartBtn = GUICtrlCreateButton("Start", 32, 64, 99, 41)
GUICtrlSetOnEvent(-1, "_Start")
Global $StopBtn = GUICtrlCreateButton("Stop", 168, 64, 91, 41)
GUICtrlSetOnEvent(-1, "_Stop")
Global $TimeLabel = GUICtrlCreateLabel("00:00:00", 104, 16, 82, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    myfunc()
WEnd

Func myfunc()
    $x=1
    While $x < 100
        If $Running Then
            ConsoleWrite("Running..." & @CR)
            $x+=1
        EndIf
        sleep(50)
    WEnd
EndFunc


Func _Stop()
    AdlibUnRegister("MyAdLibFunc")
    $Running = 0
    ConsoleWrite("-Stopped" & @CR)
EndFunc   ;==>_Stop

Func _Start()
    AdlibRegister("MyAdLibFunc", 250)
    $Running = 1
    ;myfunc()
    ConsoleWrite("+Started" & @CR)
EndFunc   ;==>_Start

Func MyAdLibFunc()
    Local Static $iCount = 0
    $iCount += 1
    $sec2time_hour = Int($iCount / 3600)
    $sec2time_min = Int(($iCount - $sec2time_hour * 3600) / 60)
    $sec2time_sec = $iCount - $sec2time_hour * 3600 - $sec2time_min * 60
    $mtime = StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)
    _GUICtrlEdit_SetText($TimeLabel, $mtime)
EndFunc   ;==>MyAdLibFunc

Func _exit()
    Exit
EndFunc

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

4 hours ago, Jos said:

So just do as I suggested !  something like this:

Jos

Sorry, my func is like this. (Contains while loops and some functions) How do I stop this using stop button?

I put """ If $running then ... endif """" in different places to see if it works, but it didn't.

By the way, I put a consolewrite in the first while loop. When I press F5, that goes to consolewrite directly. Any way to prevent that, until I click start?

#include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)
Global $Running = 0

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 286, 140, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
Global $StartBtn = GUICtrlCreateButton("Start", 32, 64, 99, 41)
GUICtrlSetOnEvent(-1, "_Start")
Global $StopBtn = GUICtrlCreateButton("Stop", 168, 64, 91, 41)
GUICtrlSetOnEvent(-1, "_Stop")
Global $TimeLabel = GUICtrlCreateLabel("00:00:00", 104, 16, 82, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    ConsoleWrite("test while 1" & @CR)
    myfunc()
WEnd

Func myfunc()
    $x=1
    While 1
        If $Running Then
            ConsoleWrite("func 1 first loop." & @CR)
            Sleep(500)
            While 1
                ConsoleWrite("2nd while loop." & @CR)
                func2()
                Sleep(500)
            WEnd

        EndIf
        sleep(1000)
    WEnd
EndFunc

Func func2()
    ConsoleWrite("this is func 2." & @CR)
    Sleep(500)
EndFunc


Func _Stop()
    AdlibUnRegister("MyAdLibFunc")
    $Running = 0
    ConsoleWrite("-Stopped" & @CR)
EndFunc   ;==>_Stop

Func _Start()
    AdlibRegister("MyAdLibFunc", 250)
    $Running = 1
    ConsoleWrite("+Started" & @CR)
EndFunc   ;==>_Start

Func MyAdLibFunc()
    Local Static $iCount = 0
    $iCount += 1
    $sec2time_hour = Int($iCount / 3600)
    $sec2time_min = Int(($iCount - $sec2time_hour * 3600) / 60)
    $sec2time_sec = $iCount - $sec2time_hour * 3600 - $sec2time_min * 60
    $mtime = StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)
    _GUICtrlEdit_SetText($TimeLabel, $mtime)
EndFunc   ;==>MyAdLibFunc

Func _exit()
    Exit
EndFunc

Thanks

Edited by SaeidN

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

  • Similar Content

    • Altor
      How I can run a function at a specified time?
      By Altor
      Hi all.
      I need execute a function at a certain time for example at 00:00 and 12:00.
      My initial idea is use a infinite loop that take the time system and compare with the specified time.
      I do not know if this is the right way to do it. I think this must have occupied the processor for a long time.
      I would appreciate if  comment on whether this is the system that you would use to do this kind of task
      or if you know  any UDF to perform this function.
      Thanks to all
    • xCobra15x
      Timer Help
      By xCobra15x
      Hi all, 
      I am looking for a quick tip on why my autoit script crashes when I run a timer. Currently I have this code:
            ....(more code above this)....
                Case $button2
                            function1()
                 Case $butTimer
                            _Timer_setTimer($hGUI, 3200, "_timedout")
              ......
               Func _timedout()
                             Msgbox(0, "Timed out", "Time is up!")
                Endfunc
      When the button is pressed, my timer starts, correct? It doesnt seem to matter if I have it on button activation or not, after the time goes by for a bit, it crashes.
      Seemingly when the time is up, my script crashes. 
      Thanks for your help!
       
       
    • Rfsvieira
      Function priority by timer
      By Rfsvieira
      Hello, i havê been reading, and thinking.. Im on my tablet só i WLL try explain what im trying to do....
      Will havê an GUI with 8 timer input that havê to set timer, but wen our if 2 our more ends at some time it havê to run 1, then outher , then outher
      Timer 1, - input 5 minutes, timer 2 - input 10, timer 3 input 15, timer 4 - input 8,......
      At some time,  timer 1 will end ate some time of timer 2 / then it at some time will end at some time timer 2 and 3 
      How i said that wen it happpens first is 2 then 1, then outhers and if 1 is done then 1st is 3, will have more than 9 input boxes that can happen it end at some time, imagine if wen timer 1= 0 function1(), then after end funciona start countdown again, if timer 2 = 0 run function2(), then after end function2 start contdoen again, if both =0 at some time first run 1 then 2, and if wen ends 1, 2,3,4 are =0 :-( :-( :-(  im lost
      This work…???
       
      thank you!
       
      Thanks for time, any ideas??? 
       
    • TheDcoder
      Wrong time being returned by the timer (Both Native Functions & Timer UDF)
      By TheDcoder
      Hello everyone
      I am encountering a strange problem with timers... i.e wrong time difference . This is an test script made by me to compare Arrays & Maps in terms of speed, I wanted publish the results in my Maps topic, so I am hiding the question in the spoiler to prevent spoilage
      Thanks in Advance! TD
       
      P.S It was hard to write in the dark spoiler  
    • Enforcer
      CPU,RAM,TIME Widgets
      By Enforcer
      Hello, just sharing my good old
      CPU, RAM, TIME widget.

      CONTY.rar