Jump to content

Time Keeping Func Stops working


Recommended Posts

Howdy friends, within the main script, which i hope doesn't matter too much, is a small function that counts a variable and marks it into the GUI.
Overall the time keeping function looks like this, excluding #includes. I added that Sleep(100) to see if that helped but i'll remove it cause i don't think it did...

Anyways, the script continues to run, the gui continues to get updated with other data that is getting filed into it, the problem is randomly the time function stops counting, or stops updating the GUI. As of writing this i just had the idea to have it write to a log so i can trace it down... 
But i'm wondering does anyone have any idea as to why this function would stop working i suppose, when the main Function launches, it does an Adlibregister("TimeUpdate",1000).
Sometimes it counts correctly for 32Hours, sometimes it stops at 1hour. Then it will just stop working, or just stop updating the GUI.
Thoughts?...

Seconds, Minutes, Hours are declared as:  global $Var = 0, FYI.

Func TimeUpdate()
$seconds+=1
GUICtrlSetData($labelTimeSeconds, ""&$Seconds)

If $seconds = 60 Then
    Global $seconds = 0
    GUICtrlSetData($labelTimeSeconds, ""&$Seconds)
    $Minutes+=1 ; 1
    GUICtrlSetData($labelTimeMinutes, ""&$Minutes)
EndIf
Sleep(100)
If $Minutes = 60 Then
    Global $Minutes = 0
    GUICtrlSetData($labelTimeMinutes, ""&$Minutes)
    $Hours+=1
    GUICtrlSetData($labelTimeHours, ""&$Hours)
EndIf

EndFunc

 

Link to comment
Share on other sites

Just out of curiosity why would you want to keep track this way when there are dozens of better ways of doing it?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

It's the only way i could think of how, i'm okay with autoit but no where what some other people are, it's my first language. I've googled and looked at other scripts but i don't understand how they are keeping time. Please by all means offer whatever you can. This is just the only and working way i found to have something even keep a somewhat track of time, by just counting a variable up. 

Link to comment
Share on other sites

you just set seconds +1 every time that function is called... that really doesn't mean anything... you don't know that that is the elapsed time, you just arbitrarily set it. I don't think you have valid data on your gui anyway due to this. you need to read the help file on Timer stuff.

TimerDiff in particular. use that to track. and assign to a variable for your time tracking

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

To how i understand it, it's working very simply off of being called every one second as an adlib, and just upping the number by one. 
TimerDiff confuses me, but i'll start playing with it more intensely if you believe that's where i should be at, thanks. :-3

Link to comment
Share on other sites

#include <Date.au3> ;_TicksToTime, _TimeToTicks

Try1()
Try2()
Func Try1()
    Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
    Local $fDiff
    For $i = 0 To 10
        Sleep(3000) ; Sleep for 3 seconds.
        $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
        ConsoleWrite(Round($fDiff / 1000) & " Seconds or " & Round($fDiff) & " Ms" & @CRLF)
    Next
EndFunc   ;==>Try1
Func Try2() ;If you need Anything more than just Seconds this is the way to go
    ;Each tick is 1000 MS
    ;Assuming 2^63 - 1 for max number in autoit
    ;should be good for approx 292471208 Years
    ConsoleWrite(_TimeToTicks(0, 0, 1))
    Local $iTicksStart = _TimeToTicks()
    Local $iHr, $iMin, $iSec
    For $i = 0 To 10
        Sleep(3000) ; Sleep for 3 seconds.
        _TicksToTime(_TimeToTicks() - $iTicksStart, $iHr, $iMin, $iSec) ; Find the difference in time
        ConsoleWrite(StringFormat("%d Hours : %d Minutes . %d Seconds", $iHr, $iMin, $iSec) & @CRLF)
    Next
EndFunc   ;==>Try2

The Second Way is far more preferable if you need H:M.S and only 1 second resolution first way is fine if you just need seconds or a higher resolution

Link to comment
Share on other sites

Atoxis,

Another way to find elapsed time (days, hours, mins, secs)...

#include <GUIConstantsEx.au3>
#include <date.au3>
#include <StaticConstants.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $gui010 = GUICreate('Elapsed Time Demo', 400, 70)
Local $lbl010 = GUICtrlCreateLabel('', 10, 10, 380, 40, BitOR($ss_center, $ss_centerimage))
GUICtrlSetFont(-1, 12, 800)
GUISetState()

AdlibRegister('incr_time', 1000)

Local $lasttime = _NowCalc()

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
    EndSwitch

WEnd

Func incr_time()

    Local $diff = _DateDiff('s', $lasttime, _NowCalc()), $days

    Local $secs = Mod($diff, 60)
    Local $mins = Mod(Int($diff / 60), 60)
    Local $hrs = Int($diff / 60 ^ 2)
    If $hrs > 23 Then
        $days = Floor($hrs / 24)
        $hrs -= $days * 24
    EndIf

    Local $diff_out = StringFormat('%03i Days %02i Hours %02i Minutes %02i Seconds', $days, $hrs, $mins, $secs)
    GUICtrlSetData($lbl010, $diff_out)

EndFunc   ;==>incr_time

kylomas

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

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