Jump to content

Recommended Posts

Posted

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.

  • Developers
Posted

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted
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)

  • Developers
Posted

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted
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

 

Posted
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.

  • Developers
Posted

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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
  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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.

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
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
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.

  • Developers
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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

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
×
×
  • Create New...