Jump to content

Simple KeepAlive Function


Zinthose
 Share

Recommended Posts

Hey all!

It's been a while since I've posted but here is a simple little function I thought I'd share that allows you to keep a script process active for a minimum amount of time.

I use this to ensure the process is active to ensure any TrayTips remain visible.  But I'm sure there is far more interesting uses for this.

How it Works

The coder calls the KeepAlive function with the number of seconds he/she wants to ensure the process will remain active for the next n seconds.  Let's say you just called the TrayTip function with 15 seconds display time.  You might call KeepAlive with 18 seconds to ensure the process stays active for the next 18 seconds.  If the process is closed after that, then the process will close normally.

#Region - KeepAlive Functions

   ;## Function used to ensure the process stays active for the next n seconds.  This is useful to ensure ToolTips ect remain visible.

   #cs - Example
      TrayTip(@ScriptName, "This Tip will be called just prior to exit of process.", 5)
      KeepAlive(8)
      Exit
   #ce - Example

   Func KeepAlive($Seconds = 5)
      Global $KeeyAlive_IsActive = False
      Global $KeeyAlive_Init = 0
      Global $KeeyAlive_Timeout = 0

      ;## Check if there is a previously set timer and only set the new value if it is more than the previously set value.
         If $KeeyAlive_Timeout - TimerDiff($KeeyAlive_Init) < $Seconds * 1000 Then
            $KeeyAlive_Init = TimerInit()
            $KeeyAlive_Timeout = $Seconds * 1000
         EndIf

      ;## If the callback is not yet regerister to execute on exit, register it now.
         If Not $KeeyAlive_IsActive Then
            $KeeyAlive_IsActive = True
            OnAutoItExitRegister("__KeepAlive_OnExit")
         EndIf
   EndFunc

   ;## Private On Exit Function used to ensure program remains active for a previously defined number of seconds.
      Func __KeepAlive_OnExit()

         ;## If closeing due to Loffoff or shutdown.  Allow it without Delay.
            If @exitMethod > 2 Then Return

         ;## Check for reasons that process should be clsoed immediatly without delay.
            If @exitMethod > 2 Or Not IsDeclared("KeeyAlive_IsActive") Or Not IsDeclared("KeeyAlive_Init") Or Not IsDeclared("KeeyAlive_Timeout") Or TimerDiff($KeeyAlive_Init) > $KeeyAlive_Timeout Then Return

         ;## Pause Process for the time remaining.
            ConsoleWrite("Keeping Alive for " & Int($KeeyAlive_Timeout - TimerDiff($KeeyAlive_Init)) & "ms" & @CRLF)
            Sleep($KeeyAlive_Timeout - TimerDiff($KeeyAlive_Init))

      EndFunc


#EndRegion

Enjoy!

--- TTFN

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

×
×
  • Create New...