Sign in to follow this  
Followers 0
Doomscape

[HELP]How to make a timer (clock)

9 posts in this topic

Hello! i want to make a timer for my installation program. like how much time it took to install it in seconds, 60 secs converted to min and 60 mins to hours.

also my STOP button doesnt work cause "program" is going on another loop like =

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $button2 <---- this doesnt work after i press button1

Exit

case $button1

"loops here"

Share this post


Link to post
Share on other sites



That means you need to change you logic and not use a closed loop without checking whether a control is pressed.

It is always a lot more helpful when you post a scriptlet that demonstrates the issue and we can work with and show you how it could be done.


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

Share this post


Link to post
Share on other sites

Well how to make a timer, i guess you dont need example script for it? eh :)

$timer = 0

and somewhere $timer = $timer + 1

but i cant make it do it like every tick.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here you go. I'm a killer for not comenting my code, but I have gone a bit overboard here. But I guess it doesn't hurt, does it? This should give you a decent idea what to do. You can also use buttons and other GUI Controls with it, this is just to demonstrate a timer and GUI On Event Mode to make your script a little easier to write.

Opt("GuiOnEventMode", 1) ;I use on event mode because I find that it makes writing my scripts easier
Global $Seconds = 0
Global $Minutes = 0
Global $Hours   = 0 ;Declaring the required functions as global or the script will crash
Global $GuiArray[2] ;I use Arrays for each different GUI just as a personal preference
$GuiArray[0]    = GUICreate("Timer", 64, 32, -1, -1, 0x80000000) ;Creating a GUI with the WS_POPUP style
$GuiArray[1]    = GUICtrlCreateLabel("0h:0m:0s", 10, 10)
GUISetOnEvent(-3, "_Exit") ;When the GuiEvent for on event mode is GUI_EVENT_CLOSE the script calls the exit function
GUISetBkColor(0x000000, $GuiArray[0]) ;Makes the background black for easier viewing against bright backgrounds
GUICtrlSetColor($GuiArray[1], 0xF0F0F0) ;Makes the text visible as white
GUISetState(@SW_SHOW, $GuiArray[0]) ;Shows the GUI
AdlibRegister("_Timer", 1000) ;Registers the function "_Timer" to run every second

While 1 ;AdLibRegister and GuiOnEvent mode dont keep the script running, so use a while loop.

WEnd

Func _Timer() ;The timer function, Use as you wish.
    $Seconds            += 1
    If $Seconds      = 60 Then
        $Minutes        += 1
        If $Minutes = 60 Then
            $Hours      += 1
            $Minutes = 0
        EndIf
        $Seconds = 0
    EndIf
    GUICtrlSetData($GuiArray[1], $Hours&"h:"&$Minutes&"m:"&$Seconds&"s") ;Sets the GUI Label to the current time.
EndFunc

Func _Exit() ;Exit function called by the GUI Event GUI_EVENT_CLOSE
    Exit
EndFunc

Also, by using GuiOnEvent mode, you can call each GUI Control's appropreate function no matter what the script is doing, and after completing that function, it will return straight back to where it was before running the function.

Edited by Mikeman27294

Share this post


Link to post
Share on other sites

Mikeman27294,

by using GuiOnEvent mode, you can call each GUI Control's appropreate function no matter what the script is doing

Not strictly true - look at the Interrupting a running function tutorial in the Wiki. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I wasnt 100% sure but I was quite confident. Thanks for the heads up, I will take a look.

EDIT

Well for the sake of correcting myself then, here is what the page on the wiki has to say;

"AutoIt queues function calls in both OnEvent and MessageLoop modes. This means that it waits until a function is finished and the code is back in your idle While...WEnd loop before running the next."

In short, the function calls are queued.

Thanks Melba23.

Edited by Mikeman27294

Share this post


Link to post
Share on other sites

Mikeman27294,

You did not read far enough into the tutorial. ;)

You can interrupt a function with an OnEvent call "as long as the function is started within the main code and not by an OnEvent call". So you were more correct than you think. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Haha thanks.

I'll just smile and pretend I knew that the whole time :)

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