Sign in to follow this  
Followers 0
xXCABLEDOGXx

Having problems with TimerINIT

7 posts in this topic

I need a function that fires every 30 to 40 minutes or so, and reading the help file, I found TimerINIT and TimerDIF. I wrote a little script that looks something like this to test with:

$CanBePerformedAgain_YN = 1                                     ; Set it as a 1 by default for script start
While 1=1                                                       ; Loop Endlessly for testing
    Select
        Case $CanBePerformedAgain_YN = 1                        ; As long as $CanBePerformedAgain = 1, fire this function
            PerformThisFunction()                               ; The function to fire
            $CanBePerformedAgain_YN = 0                         ; Now that it's been fired, change the toggle bit to 0
            $Timer_Innitialize = TimerInit()                    ; Innitialize a timer
        Case $CanBePerformedAgain_YN = 0                        ; If the same variable = 0, then we have to check to see if it's time to fire again
            $Timer_Difference = TimerDiff($Timer_Innitialize)   ; Check the time passed since the time stamp
            If $Timer_Difference >= 60000*30 Then               ; If the difference it greater than or equal to 30 minutes, then...
                $CanBePerformedAgain_YN = 1                     ; Toggle the value of $CanBePerformedAgain_YN to 1 so we can fire the function
            EndIf
    EndSelect
Wend

I'm actually going to be using this to create an Anti-AFK/Anti Screen saver on my laptop for work. Because of security issues and reasons, we are constantly having to log back into our VPN, and it is very time consuming and annoying, and I am often away from my laptop for 40 minutes or more in the field.

This script is supposed to choose the best match based on the toggle variable, right? Because it doesn't, instead it attempts to fire the function over and over again, ignoring the case arguements all together. By default once the script fires, it sets the flag to yes (1), and then once the function fires, it sets the flag to 0, my understanding is, as long as the toggle remains 0, the first case shouldn't fire at all. But it does.

I've tried this several ways, this seemed the most logical, though all of my tests have failed so far. Any ideas?

Share this post


Link to post
Share on other sites



Try it this way.

PerformThisFunction() ; Run the function once before entering the loop, this can be taken out if you don't want it to run as soon as it starts.
$Timer_Innitialize = TimerInit() ; Initialize the timer before going into the loop
While 1  ; Loop Endlessly for testing
    If TimerDiff($Timer_Innitialize) >= 180000 Then ; Check if 30 minutes has passed yet
        PerformThisFunction() ; Fire the function after 30 minutes has passed
        $Timer_Innitialize = TimerInit() ; ReInitialize the timer
    EndIf
    Sleep(100) ; added so that the script doesn't eat up your CPU cycles.
WEnd
Func PerformThisFunction()
    ConsoleWrite("PerformThisFunction" & @CRLF)
EndFunc   ;==>PerformThisFunction

Much shorter and it eliminates the need for the select statements.


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

Share this post


Link to post
Share on other sites

Try it this way.

PerformThisFunction() ; Run the function once before entering the loop, this can be taken out if you don't want it to run as soon as it starts.
$Timer_Innitialize = TimerInit() ; Initialize the timer before going into the loop
While 1  ; Loop Endlessly for testing
    If TimerDiff($Timer_Innitialize) >= 180000 Then ; Check if 30 minutes has passed yet
        PerformThisFunction() ; Fire the function after 30 minutes has passed
        $Timer_Innitialize = TimerInit() ; ReInitialize the timer
    EndIf
    Sleep(100) ; added so that the script doesn't eat up your CPU cycles.
WEnd
Func PerformThisFunction()
    ConsoleWrite("PerformThisFunction" & @CRLF)
EndFunc   ;==>PerformThisFunction

Much shorter and it eliminates the need for the select statements.

Thanks, that should work fine.

Have another question, if I may... strings...

If I setup a string like this, it works perfectly fine:

$mystring = "LOGGED xXCABLEDOGXx is at work now" - or -

$mystring = "LOGGED " & $myname & " is at work now"

But if I break the strings up to make them easier to modify like this, they are ignored:

$myname = "xXCABLEDOGXx"

$Logged = "LOGGED"

$MSG = " is at work now"

$prefix = $Logged & $myname

$mystring = $prefix & $MSG

Is there a reason for this? I mean it's no big deal to create a string that states:

$prefix = "LOGGED xXCABLEDOGXx"

$MSG = " is at work now"

$prefix = $Logged & $myname

$mystring = $prefix & $MSG

That will work fine. I was just wondering about the middle example there. Because it's ignored when I parse the chat log of my office communicator, while either of the others work fine. Just didn't understand it because it doesn't go beyond the limits of how many characters can be stored in a string, and I've "chalked" it up to the fact that I may have simply "stacked" strings too deep to be seen (but even in my overthinking logic, that doesn't make sense either).

Anyhow, thanks for the first question answered :unsure:

Share this post


Link to post
Share on other sites

I took your example and made it into a short script and it's displaying the string correctly at the end.

$myname = "xXCABLEDOGXx"
$Logged = "LOGGED "
$MSG = " is at work now"
$prefix = $Logged & $myname

$mystring = $prefix & $MSG
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $mystring = ' & $mystring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

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

Share this post


Link to post
Share on other sites

I took your example and made it into a short script and it's displaying the string correctly at the end.

$myname = "xXCABLEDOGXx"
$Logged = "LOGGED "
$MSG = " is at work now"
$prefix = $Logged & $myname

$mystring = $prefix & $MSG
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $mystring = ' & $mystring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console

Don't have access to my laptop right now, so I threw this together to better illustrate my meaning.

Global $line
Global $CurrentLogFile
Global $CurrentLine
Global $LastLine
Global $LastLineNumber

Global $Username
Global $IsLogged
Global $Prefix
Global $ChatTrigger

$CurrentLogFile = "mscchatlog.txt"
$CurrentLine    = 1
$Username       = "Supervisor"
$IsLogged       = "Logged"
$Prefix         = $IsLogged & $Username
$ChatTrigger    = "Are you there"


While 1
    Check4Trigger()
    Sleep(120000)
WEnd

Func Check4Trigger()
    If $LastLine = $Prefix & $ChatTrigger Then
        Send ("{ENTER}I am currently unavailable at this time{ENTER}")
        Sleep(1000)
    EndIf
EndFunc

Func GetLastLine()
    While 1
        $line = FileReadLine($CurrentLogFile, $CurrentLine)
        If @error = -1 Then ExitLoop
        $CurrentLine = $CurrentLine + 1
    WEnd
    $LastLineNumber = $CurrentLine - 1
    $LastLine = FileReadLine($CurrentLogFile, $LastLineNumber)
EndFunc

Now, given the previous examples of what I said, "worked", this does not. Regardless if it returns the right value in your test, in this script, what it does is ignore the string all together. BUT if I used one of the other examples, it catches it all the time. The reason I was trying to stack the variables was to make it easier if I distro the script to other people, they only need to change one or two things. Instead of searching through my script and finding what and where to change.

I will go back and try it again next week, but I wanted to make sure that I wasn't missing something in the translation.

Share this post


Link to post
Share on other sites

You're missing spaces between LOGGED and the username, so that in your last example $prefix would equal LOGGEDSupervisor. Your other examples all show a space after the word LOGGED and before the username, whether it's a variable holding the username or if you implicitly put it in there. Your example that I wrote up into the short script has the same condition, which I missed and added the space after logged in my example.


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

Share this post


Link to post
Share on other sites

You're missing spaces between LOGGED and the username, so that in your last example $prefix would equal LOGGEDSupervisor. Your other examples all show a space after the word LOGGED and before the username, whether it's a variable holding the username or if you implicitly put it in there. Your example that I wrote up into the short script has the same condition, which I missed and added the space after logged in my example.

wow... I can't believe I missed that... I bet that's it exactly. As careful as you try to be, and as thorough, death by omission of a simple [ ] (space)...

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