Sign in to follow this  
Followers 0
b3lorixx

Help with timer

10 posts in this topic

#1 ·  Posted (edited)

Basically i want to run a function every 5 minutes without the use of a while or sleep currently i am tryin to use this:

Local $Timer = iniread("wrdtc-settings.ini", "Timer",  "current", "0")
func Getinfo()
$current = TimerInit()
$NewCur = $Timer + 500000
If $NewCur > $current Then
    IniWrite ("wrdtc-settings.ini", "Timer",  "current", $current)
EndIf
EndFunc

but its not working and i cant figure out what else to do..

Edited by b3lorixx

Share this post


Link to post
Share on other sites



I assume that there is an AdLibRegister() function that is calling GetInfo(), or GetInfo() is being called from somewhere in another While loop. The problem is that each time it is called, the $Current value is reset by TimerInit() and it will always be smaller than $NewCur, even if $Timer is 0 (the value of $NewCur will be at a minimum of 10000, and it will only take milliseconds to go through the function lines, and since $Current is always less than $NewCur, that millisecond time will be written to the INI file)

If you want to accomplish this without a formal While Loop, you will thry something like this:

Global $Current = TimerInit()   ;must be declared BEFORE calling the function for the first time
AdLibRegister("Getinfo")

;Your script, which must in someway never end, either with a while or its' natural progress

Func Getinfo()
    If TimerDiff($Current) < 300000 Then Return ;Skip this time and go back to main script
    ;Otherwise, do your stuff here
    
    ;If you need to return something from the function, do this:
        ;$Current = TimerInit() ;reset the timer since the function has run this time
        ;Return ;whatever you need to return
        
    ;If you do not need to return anything from the function, do this:
        Return $Current = TimerInit()   ;reset the timer since the function has run this time
EndFunc   ;==>Getinfo

Share this post


Link to post
Share on other sites

AdlibRegister("Getinfo", 5 * 60 * 1000); Run every 5 mimutes
$StopHour = @HOUR + 1; The Hour now plus one our when you want to end the loop!
Do;To keep something going until you want to end it
Until @HOUR = $StopHour
Func Getinfo()
    MsgBox(0, '', 'It is time to do something!', .5)
EndFunc   ;==>Getinfo

Basically i want to run a function every 5 minutes without the use of a while or sleep

Basically you can not repeat a task if you do not have some sort of loop.

This working example does not use any file to read anything but wil run every 5 minutes. It wil stop the loop after an hour.


Share this post


Link to post
Share on other sites

I assume that there is an AdLibRegister() function that is calling GetInfo(), or GetInfo() is being called from somewhere in another While loop. The problem is that each time it is called, the $Current value is reset by TimerInit() and it will always be smaller than $NewCur, even if $Timer is 0 (the value of $NewCur will be at a minimum of 10000, and it will only take milliseconds to go through the function lines, and since $Current is always less than $NewCur, that millisecond time will be written to the INI file)

If you want to accomplish this without a formal While Loop, you will thry something like this:

Global $Current = TimerInit()   ;must be declared BEFORE calling the function for the first time
AdLibRegister("Getinfo")

;Your script, which must in someway never end, either with a while or its' natural progress

Func Getinfo()
    If TimerDiff($Current) < 300000 Then Return ;Skip this time and go back to main script
    ;Otherwise, do your stuff here
    
    ;If you need to return something from the function, do this:
        ;$Current = TimerInit() ;reset the timer since the function has run this time
        ;Return ;whatever you need to return
        
    ;If you do not need to return anything from the function, do this:
        Return $Current = TimerInit()   ;reset the timer since the function has run this time
EndFunc   ;==>Getinfo

yes but $Timer isnt 0, its set to be the current time after you run the configuration form that MUST be run. So i was using $NewCur = $Timer + 500000 to add 5 minutes to $timer to make sure the function wasnt running until it was 5 minutes past the last run

Share this post


Link to post
Share on other sites

AdlibRegister("Getinfo", 5 * 60 * 1000); Run every 5 mimutes
$StopHour = @HOUR + 1; The Hour now plus one our when you want to end the loop!
Do;To keep something going until you want to end it
Until @HOUR = $StopHour
Func Getinfo()
    MsgBox(0, '', 'It is time to do something!', .5)
EndFunc   ;==>Getinfo

Basically you can not repeat a task if you do not have some sort of loop.

This working example does not use any file to read anything but wil run every 5 minutes. It wil stop the loop after an hour.

yes, but i cant delay my While loop for long because im using it to keep a connection alive

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

yes, but i cant delay my While loop for long because im using it to keep a connection alive

The easy way ! Posted Image

#include <Date.au3>

$_InitTime = _NowCalc ( )

While 1   ; your script Loop
    
    $_DateCalc = _DateDiff ( 'n', $_InitTime, _NowCalc ( ) )
    If $_DateCalc >= 5 Then
        MsgBox ( 4096, "", "5 Minutes elapsed ! " & $_DateCalc )
        $_InitTime = _NowCalc ( )
    EndIf

WEnd
Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

yes but $Timer isnt 0, its set to be the current time after you run the configuration form that MUST be run. So i was using $NewCur = $Timer + 500000 to add 5 minutes to $timer to make sure the function wasnt running until it was 5 minutes past the last run

TimerInit() returns an Epoch timestamp.

The timestamp for "Thu, 27 Jan 2011 08:40:40 GMT" (3:40 AM EST) is 1296117640

If you are increasing $Timer by 50000 each time the function is called, it will take 1296117640 / 50000 or 2592.23528 calls for $timer to eqaul that timestamp. Actually it will take more because you compare $Timer to the current timestamp (which also increases)

yes, but i cant delay my While loop for long because im using it to keep a connection alive

In the event that you do get the timer correct, what kind of function can you run within the script that will not upset your super-sensitive existing While loop? Why not just do the TimerInit() at the beginning of the script, make a function that checks if it has been 5 minutes ( with TimerDiff() ), and if so, (1)start an external script and (2) reset the timer?

The easy way ! Posted Image

The OP requires that no extra While loops be used

What is the point of using TimerInit() if you never call TimerDiff()?

Share this post


Link to post
Share on other sites

The OP requires that no extra While loops be used

What is the point of using TimerInit() if you never call TimerDiff()?

His last post : yes, but i cant delay my While loop for long because im using it to keep a connection alive

I place it in his Own while loop and don't use any TimerInit()...Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

His last post : yes, but i cant delay my While loop for long because im using it to keep a connection alive

I place it in his Own while loop and don't use any TimerInit()...Posted Image

I figured it out after a few hours of trial and error. i did try to put the entire code into the while loop. but it was bugging and just constantly spammed lol

Share this post


Link to post
Share on other sites

I figured it out after a few hours of trial and error. i did try to put the entire code into the while loop. but it was bugging and just constantly spammed lol

You want more help, I need entire code...


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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