Zinthose

Simple KeepAlive Function

1 post in this topic

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!

1 person likes this

--- TTFN

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

  • Similar Content

    • CurtimusPrime
      By CurtimusPrime
      #include <Date.au3> #include <TrayConstants.au3> HotKeySet("^d", "WhatIsToday") Global $Today = _Date_Time_GetSystemTime While 1 Sleep(100) WEnd Func WhatIsToday() TrayTip("Today's date", "Today is... " & $Today, 8) Sleep(8000) TrayTip("", "", 0) EndFunc ;==>WhatIsToday all it outputs into tray bubble is:  Today is...
      without getting the system time as text
    • pau31
      By pau31
      Hello everyone,
      I've just upgraded my SO to Windows 10 and I've noticed that the function TrayTip() generates this different type of message:

      The problem is that one traytip command does not immediately dismiss the previous one, so it takes forever. I've also tried this: 
      But it doesn't work :/
      Anyone has been throught this problem? Or can you recommend me an alternative way to inform the user about the status of a script?
      Thank you very much!
    • careca
      By careca
      Works with ctrl+left mouse click, displays a traytip, balloon-like with the image resolution, if can be read. Not entirely mine, got help from forum, without you i couldn't have done it. thanks ppl.


      #include "image_get_info.au3" #include <Misc.au3> AutoItSetOption("TrayAutoPause", 0) Local $DLL = DllOpen("user32.dll") Local $file, $aInfo, $keyR1, $keyR2 RegWrite("HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerAdvanced", "EnableBalloonTips", "REG_DWORD", "1") While 1 Sleep(50) If _IsPressed("11", $dll) Then ;CTRL $keyR1 = 1 CTRLkey() ElseIf $keyR1 + $keyR2 = 2 Then RES() EndIf WEnd DllClose($dll) Func CTRLkey() If _IsPressed("01", $dll) Then ;LMB $keyR2 = 1 EndIf EndFunc ;==>CTRLkey Func RES() $keyR1 = 0 $keyR2 = 0 Send("^c") Sleep(50) $file = ClipGet() $aInfo = _ImageGetInfo($file) TrayTip("Resolution", _ImageGetParam($aInfo, "Width") & "x" & _ImageGetParam($aInfo, "Height"), 2, 1) EndFunc ;==>RES
      EDIT: Required UDF attached, had to change code as it wasn't working flawlessly as it should, now it's good, press ctrl and click on the image, once you release ctrl, the traytip will show the resolution of the image. Got to investigate why it wasn't working well the other way.
      image_get_info.au3
      AutoIt Res.au3
    • Command3r
      By Command3r
      hello, i made a script to show a traytip every "10 SECs" and timeout is "2 SECs", but i don't know why it doesn't hide after 2 SECs or even 1000 sec,, i must click on it to hide

      check the script:


      While 1 $start = TimerInit() Sleep(10000) $End = TimerDiff($start) $rEND = Round($End / 1000, 0) If $rEND = "10" Then $x = TrayTip("test", "testtt", 2) Sleep(2000) EndIf WEnd any help would be appreciated
      thanks.
    • Roshith
      By Roshith
      Hi,
      When I use traytip with timeout 20 secs, sometimes it stays for 20secs or more, but sometimes it fades away fast (less than 5 secs). What is the reason for this. Is there any work around.

      TrayTip("Title", "Text", 20)
      Sleep(30000)

      Roshith