Jump to content

Timer


SlimShady
 Share

Recommended Posts

I created a timer that uses little memory and CPU time.

How it works:

You press on "Start/Stop": It saves the current time (hours, minutes, seconds)

Then it doesn't do a thing until you press once more on "Start/Stop".

And you can press "Clear" to clear and reset the timer.

If you click on the label control, it shows the current time and shows how many seconds, minutes, hours have passed since the start.

You can click as many times as you want on the label.

Download it below.

http://www.autoitscript.com/fileman/users/public/SlimShady/Timer.au3

<offtopic>Did I spell "label" right?

The helpfile doesn't say "lable". So I guess I spelled it right, then.

</offtopic>

Edited by SlimShady
Link to comment
Share on other sites

Good Idea

But wouldn't it be more easy and less code to convert the time all in seconds so you can do better math on it as to put it into strings. In the end you can put the elapsed seconds back to h:m:s

FileChangeDir(@ScriptDir)
GUICreate("Timer", 120, 200, 2, 2)

$start = GUISetControl ("button", "Start", 10,10,100,20)
GuiSetControlNotify()
$stop = GUISetControl ("button", "Stop", 10,30,100,20)
GuiSetControlNotify()
$label_1 = GuiSetControl("label", "Klick Start to start timer",  10, 60, 100, 100, 0x1000)
;-------------------------------------------------
$start_sec = 0;---initial code---

GuiShow()
While GUIMsg() <> -3
$msg = GuiRead()
Select

case $msg = $start
$start_sec = _now()
GuiWrite($label_1, 0, "Timer runs" & @CRLF & "Starttime(sec):" & @CRLF & $start_sec)

case $msg = $stop
If $start_sec <> 0 Then
 $stop_sec = _now()
 $elap_sec = $stop_sec - $start_sec
 GuiWrite($label_1, 0, "Timer stopped" & @CRLF & "Stoptime(sec):" & @CRLF & $stop_sec & @CRLF & "Elapsed seconds:" & @CRLF & $elap_sec)

 $elap_hours = Int($elap_sec / 3600)
 $elap_rest_sec = Mod($elap_sec, 3600)
 $elap_minutes = Int($elap_rest_sec / 60)
 $elap_seconds = Mod($elap_rest_sec, 60)

 MsgBox (0, "Elapsed Time", $elap_hours & "h " & $elap_minutes & "m " & $elap_seconds & "s")
 GuiWrite($label_1, 0, "Klick Start to start timer")
 $start_sec = 0
Else
 GuiWrite($label_1, 0, "You have to klick Start first")
EndIf


EndSelect
Wend

Func _now()
$sec_hour = @HOUR * 3600
$sec_min = @MIN * 60
$sec_sec = @SEC
$sec_all = $sec_hour + $sec_min + $sec_sec
return $sec_all
EndFunc

This timer will not run over a new day.

I searched for a floor function wich I know from PHP. Took some time till I discovered the Int in the helpfile in "Variables and Conversion". Shouldn't it be also in "Math Functions"?

arctor

Link to comment
Share on other sites

I wonder why I can't close this from the standard close ('X') button on upper right corner? I don't know much about GUI functions (yet), but a quick scan through the latest help file would suggest that the code is correct ($msg -3). Can you please confirm if this works for you?

Maybe I'm having an earlier 'unstable' version than you?

Link to comment
Share on other sites

I wonder why I can't close this from the standard close ('X') button on upper right corner? I don't know much about GUI functions (yet), but a quick scan through the latest help file would suggest that the code is correct ($msg -3). Can you please confirm if this works for you?

Maybe I'm having an earlier 'unstable' version than you?

I think that's the case...

It works using the beta version from June, 22nd.

Link to comment
Share on other sites

would be nice to see it count out the time as it goes.. so I added my two cents :huh2:

However I was lazy and didn't put in your start and stop buttons.. you can always add those back in though :)

#include <GUIConstants.au3>

GuiCreate("Example", 200, 200)

$label_1 = GuiSetControl("label", "H0 M0 S0",  10, 5, 100, 20, 0x1000)
 $button = GuiSetControl("button", "Exit", 0, 100, 100, 20)
GuiShow()

$A=@sec
$sec=0
$min=0
$hour=0
While 1
   
   $msg=GUImsg(0)
   
   if $msg=$button then ExitLoop
      if $msg="-3" then Exit
      
   if @sec > $A then $sec=$sec+1
      
      $A=@SEC
      
      If $sec=60 then 
         $min=$min+1
         $sec=0
         EndIf
         If $min=60 then 
            $hour=$hour+1
            $min=0
            EndIf
            
      $count=string($hour & " : " & $min & " : " & $sec)
      GUIWrite($label_1,0,$count)
      
   Wend

Thanks for the inspiration.. it was fun figuring this out. :D

moo

Link to comment
Share on other sites

My opinion:

I don't like to see the seconds running. Whether it's a digital or analog clock.

And our scripts use minimum resources this way.

@Arctor: I dropped my script and I'm using yours now.

I changed it to the way I like it.

When you click on the label, the timer starts, once more and the timer stops.

Clear button clears the label and you can start over.

You don't have to press 'clear' to start over, just click once more on the label to start the timer.

See for yourself:

http://www.autoitscript.com/fileman/users/public/SlimShady/Timer.au3

Link to comment
Share on other sites

When you click on the label, the timer starts, once more and the timer stops.

Cool. One Button less.

I never had the idea to use a label for notifying. Learning is great.

If you want to get it real puristic you can get rid of the clear button too and use the label for all three states.

arctor

Link to comment
Share on other sites

Instead of starting the time each time you press, Start, you can have stop time. (or what is called)

So:

Start ; Starts the timer

Start ; Set a "middle" time

Start ; Set another "middle" time (you should be able to do this as many times as you like

Stop : Stops the timer

(Start) :Start the timer from were you stopped it.

Stop, Stop ; Clear the timer.

Just my 2 cent, i'm a bit to busy to make my version, but now you got my idea :D

Were ever i lay my script is my home...

Link to comment
Share on other sites

And our scripts use minimum resources this way.

I dunno about you but my CPU activity didn't crawl above the 1% mark it was already sitting at before I started the script. and my ram jumped a whole 800k.. no more than any other script. So system resources are not really an issue I think

reasons that visual feedback is usefull.

1. you are counting the time for more than one action .. this could also be done though with a print time button so rather than stopping the count it posts the currunt count and keeps going

2. With time sesitive items you do not need to wait untill the function is comleted to know if it's going slower than the last one and thus do not have to finish the task. ie if the first test turns up 23 seconds and then next one is 25.. in your mind you are not nessesarily going to know it's slower withount confirmation of the read out.. (unless you have a very good sense of time or you were good at counting them off.. in which case you don't really need this tool now do you?) So really my point is that if the time hit 23 regardless of how long the process would take.. you could stop.. make changes and try again without having to wait untill it was done and compare times. get my meaning?

3. is just a matter of inquisitive or impatient minds.. you have a function that takes 30 mins say. You set the timer and then go do something else.. then come back to check it's progress. if it's close to the 30 min mark you can sit down and wait for it to complete the task so you can mark down the time. However if it's say only at the 20 min mark or 15 mins .. you can still wander about and do more things while you wait. however this can also be gained from a Print time button.

So I guess what I'm saying is it would be usefull to have a print time/time stamp tool even if you don't want to have a constant read out.

moo

Link to comment
Share on other sites

here.. tried again based on your code.. I changed some things like the way time was calculated so it made more sense to me. I tend to do that so that I'm positive I know what each section does.

this time there is no constant read out but you can print the time without clearing the clock.

Probably could have done this a whole lot easier and with less changes but simply duplicating the stop function and not clearing the clock.. but I had to see what other way I could do this based on what you were doing. (I don't use functions often but they seem to be a great thing for GUI buttons)

#include <GUIConstants.au3>
FileChangeDir(@ScriptDir)
GUICreate("Timer", 200, 200, 2, 2)

Opt("GuiNotifyMode",1)
$start = GUISetControl ("button", "Start", 10,10,100,20)
$print = GUISetControl ("button", "print", 10,30,100,20)
$stop = GUISetControl ("button", "Stop", 10,50,100,20)

$label_1 = GuiSetControl("label", "Klick Start to start timer",  10, 80, 100, 60, 0x1000)
;-------------------------------------------------
$start_sec = 0;---initial code---

GuiShow()
While GUIMsg() <> -3
$msg = GuiRead()
Select

case $msg = $start
$hour=@HOUR
$min=@MIN
$sec=@SEC
$start_sec = _now()
GuiWrite($label_1, 0, "Timer runs" & @CRLF & "Starttime(sec):" & @CRLF & $start_sec)

case $msg = $print
If $start_sec <> 0 Then
$stop_sec = _count()
GuiWrite($label_1, 0, "Timer stopped" & @CRLF & "Stoptime(sec):" & @CRLF & $stop_sec )

Else
GuiWrite($label_1, 0, "You have to klick Start first")
EndIf

case $msg = $stop
If $start_sec <> 0 Then
$stop_sec = _count()
GuiWrite($label_1, 0, "Timer stopped" & @CRLF & "Stoptime(sec):" & @CRLF & $stop_sec )


MsgBox (0, "Elapsed Time", $stop_sec)
GuiWrite($label_1, 0, "Klick Start to start timer")
$start_sec = 0
Else
GuiWrite($label_1, 0, "You have to klick Start first")
EndIf

EndSelect
Wend

func _now()
$now=string(@hour & " : " & @min & " : " & @SEC)
Return $now
EndFunc

Func _count()
$sec_hour = @HOUR - $hour
$sec_min = @MIN - $min
$sec_sec = @SEC - $sec
$time=string($sec_hour & " : " & $sec_min & " : " & $Sec_sec)
return $time
EndFunc

I know it's messy since I didn't clean up everything (ie the name $sec_XXX I know means hour's as seconds and mins as seconds etc me=lazy) .. oh well.. I'm an artist not a coder :D

moo

Edited by cowsmanaut
Link to comment
Share on other sites

...

if the time hit 23 regardless of how long the process would take.. you could stop.. make changes and try again without having to wait untill it was done and compare times. get my meaning?

...

I read this part 6 times and I still can't understand.

Can you please make it more understandable?

Edited by SlimShady
Link to comment
Share on other sites

I changed some things like the way time was calculated so it made more sense to me. I tend to do that so that I'm positive I know what each section does.

Hi cowsmanaut,

did you try your script? Sorry to say but your script will definitely not work.

It will not survive the first minute change.

This is done when you start the timer:

$hour = @HOUR
$min = @MIN
$sec = @SEC
And you call this function when you want to print the elapsed time.
Func _count()
$sec_hour = @HOUR - $hour
$sec_min = @MIN - $min
$sec_sec = @SEC - $sec
$time=string($sec_hour & " : " & $sec_min & " : " & $Sec_sec)
return $time
EndFunc
So lets assume startime is 2:50:45

And at 3:02:04 you call your time calculating function (that makes more sense to you)

Then this is what the function does:

$sec_hour = 3 - 2

$sec_min = 2 - 50

$sec_sec = 4 - 45

Does it still make sense to you? Do you think you will get back the elapsed time?

And BTW: The only button that works is the start button. This code is completely out of control.

Check the code I posted or SlimShady's last Timer.au3 and see why it makes sense to convert the start and stop time to full seconds.

arctor

Link to comment
Share on other sites

I read this part 6 times and I still can't understand.

Can you please make it more understandable?

LOL.. yeah that would be the typical response for my explainations :)

ok..

you run a task and time it.. the resulting time to completion is 23 seconds. You think you can improve on that time by adusting say the order of the tasks youve done. So you try that and time it once again.. however with your current set up you need to wait for the function to complete before you can stop the timer because you don't know how much time has passed and have no way of checking. So you must wait untill it's done. If however you could periodically check the time passed you could determine if it's already passed 23 mins and thus give up and try again instead of waiting around to see the final read out. Make more sense?

arctor

yes I did try it out.. 10 times.. on two different computers. Ran fine. first few tests went for about 20sec-2mins so I'm fairly sure it goes past the first minute change as you suggest. and all the buttons work as they are intended to.. (edit just tried two more machines at work they too work. ss 1 laptop and 3 desktops of different manufacture)

The math however is obviously sloppy. since I did not take into thought what you mentioned there. Is this what you mean by first minute change? Not that it won't run but the count would be inaccurate after you started calculating mins?

I understand better why you need to make the conversion to seconds now, yes. since it gives a total count. My thought process was obviously a little too simple thinking that I could subtract the "larger time" from the first "smaller time" as I had it in my head :huh2:

well, l'm sure there must be other ways of doing all this.. I'll give it some thought and see if I can come up with annother way that works. Not because I think it would be better than your way. but just to work my brain around the idea and make sure I learn something from the process. I like to think of these things as puzzles.. makes it more fun :lol:

I'm just an artist by trade.. I've not programmed since the days of the commodore 64.. I tried amiga basic but it crashed too much. Anyway, the best thing I get from reworking these things is what I learn in the process so I can further understand how it works.. my brain is not yet used to thinking in this way (as I'm sure you can guess) it's an invaluable tool set though since I work in 3D and XSI has a scripting language that is based also on VBscript.

Thank you so very much for your patience and guidance! It's greatly apreciated :D

Edited by cowsmanaut
Link to comment
Share on other sites

@cowsmanaut: The math however is obviously sloppy. since I did not take into thought what you mentioned there. Is this what you mean by first minute change?

Yes, that's what I meant.

I had problems to understand your post too. But I think I got it a liitle. You want to have something like a lap time or how it's called. A button which can call the elapsed time without changing the starttime. Is that write?

arctor

BTW: I am going on vacation now for a week. So I can have a look at your answer only when I am back.

arctor

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...