Jump to content
Sign in to follow this  
DefinedV

Program Runtime

Recommended Posts

DefinedV

I would like to keep track of program runtime on exit, how would I go about doing so in AutoIt?

-Thanks

Share this post


Link to post
Share on other sites
AdmiralAlkex

Hi and Welcome to the forum!

Do you mean how long the script has been running?

Maybe something like this fits:

#include <Date.au3>

Global $iStartTime = _Now()
OnAutoItExitRegister("_OnExit")

MsgBox(0, "", "Wait a while and close me")

Func _OnExit()
    ConsoleWrite(_DateDiff("s", $iStartTime, _Now()) & @CRLF)
EndFunc

The function registered with OnAutoItExitRegister() will run when the script gracefully exist. If you want to get the time even if it get closed from "task manager" and such tools it would be more advanced.

Share this post


Link to post
Share on other sites
DefinedV

Hi and Welcome to the forum!

Do you mean how long the script has been running?

Maybe something like this fits:

#include <Date.au3>

Global $iStartTime = _Now()
OnAutoItExitRegister("_OnExit")

MsgBox(0, "", "Wait a while and close me")

Func _OnExit()
    ConsoleWrite(_DateDiff("s", $iStartTime, _Now()) & @CRLF)
EndFunc

The function registered with OnAutoItExitRegister() will run when the script gracefully exist. If you want to get the time even if it get closed from "task manager" and such tools it would be more advanced.

Thanks, that's exactly what I was looking for. Was hoping there was a solution better then converting the time from differences in hour/minute/second, and this seems much more convenient. Although, is there a way to display it with a msgbox? I tried but it always yielded a value of 0. My code is below.

Func _OnExit()
    $end = (_DateDiff("s", $iStartTime, _Now()) & @CRLF)
    msgbox(0,"",$end)
EndFunc

-Thanks

Share this post


Link to post
Share on other sites
AdmiralAlkex

If you didn't just let it run for 0 seconds I don't know. Take this for example, the msgbox says 3.

#include <Date.au3>

Global $iStartTime = _Now()
OnAutoItExitRegister("_OnExit")

Sleep(3000) ;Do nothing for 3 seconds

Func _OnExit()
    $end = _DateDiff("s", $iStartTime, _Now())  ;"()" not really needed and @CRLF seemed pointless there.
    msgbox(0,"",$end)
EndFunc

Share this post


Link to post
Share on other sites
somdcomputerguy

I'm still running with 3.3.4.0, not the most current version, and _DateDiff seems not to function properly with _Now(), but it does work correctly with _NowCalc().

Edit: however it could be my PCs date/time format is incorrect format for _DateDiff..

Another Edit:

ConsoleWrite(_Now() & @LF)
ConsoleWrite(_NowCalc() & @LF)

Returns

3/9/2010 9:31:43 PM
2010/03/09 21:31:43

So it's not a version info..

Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites
DefinedV

Ahh thanks guys. It's working now with:

#include <Date.au3>

Global $iStartTime = _NowCalc()
OnAutoItExitRegister("_OnExit")

Sleep(3000) ;Do nothing for 3 seconds

Func _OnExit()
    $end = _DateDiff("s", $iStartTime, _NowCalc())  ;"()" not really needed and @CRLF seemed pointless there.
    msgbox(0,"",$end)
EndFunc

Ahh is there any way to convert the output to something similar to HH:MM:SS without writing my own function?

Edited by DefinedV

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.