Sign in to follow this  
Followers 0
Mixam

Is there a way to cancel a sleep() ?

12 posts in this topic

I'm looking for ideas on how best to go about canceling a sleep time. Basically if my script is sleeping for an hour or something I want to be able to hit a reload button or something that kills the current sleep and then goes back to the main loop. The way I'm thinking of doing it is instead of having sleep(3600000) have something like:

For $b = 1 To 3600
    If $reload = True Then ExitLoop
    Sleep(1000)
Next

But it would be so much easier if there was just a way to kill the current sleep or something. Any Ideas? Like is there a type of while loop where as soon as the expression is false it stops all the code in the loop instead of waiting till it all executes?

Share this post


Link to post
Share on other sites



Look in Hotkeyset()

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Or you can use Opt('GUIOnEventMode', 1). Event actions interrupt sleeps.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Share this post


Link to post
Share on other sites

I'm looking for ideas on how best to go about canceling a sleep time. Basically if my script is sleeping for an hour or something I want to be able to hit a reload button or something that kills the current sleep and then goes back to the main loop. The way I'm thinking of doing it is instead of having sleep(3600000) have something like:

For $b = 1 To 3600
    If $reload = True Then ExitLoop
    Sleep(1000)
Next

But it would be so much easier if there was just a way to kill the current sleep or something. Any Ideas? Like is there a type of while loop where as soon as the expression is false it stops all the code in the loop instead of waiting till it all executes?

An example

$haltsleep = False
HotKeySet("^{F8}", "WakeUp");Ctrl F8


mySleep(3600000);sleep for 1 hour unless woken up
ConsoleWrite("woke up" & @CRLF)


Func mySleep($tt)
    Local $begin = TimerInit()
    
    While TimerDiff($begin) < $tt
        If $haltsleep Then
            $haltsleep = False
            Return
        EndIf
        Sleep(20)
        
    WEnd
EndFunc  ;==>mySleep

Func WakeUp()
    $haltsleep = True
    ConsoleWrite("stopping" & @CRLF)
EndFunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

I am already using TrayOnEventMode By interupts sleep do you mean it pauses the sleep or ends it? I need it to end that sleep and go on with the function its in or to move on to a diff function I would specify

@Martin: Thanks, that will be very useful to me if oneventmode doesn't do what I want it to.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Interrupt as in it'll wake it up. See this slight modification of martin's code:

#Include <Constants.au3>

Opt("TrayAutoPause", 0)
Opt("TrayOnEventMode", 1)

TraySetOnEvent($TRAY_EVENT_MOUSEOVER, "WakeUp")

$haltsleep = False

mySleep(3600000);sleep for 1 hour unless woken up
ConsoleWrite("woke up" & @CRLF)

Func mySleep($tt)
    Local $begin = TimerInit()
    
    While TimerDiff($begin) < $tt
        If $haltsleep Then
            $haltsleep = False
            Return
        EndIf
        Sleep(20)
        
    WEnd
EndFunc  ;==>mySleep

Func WakeUp()
    $haltsleep = True
    ConsoleWrite("stopping" & @CRLF)
EndFunc

As for whether it'll cancel the sleep, or pause it, that depends on how you implement it. In martin's code, it'll stop, because the custom sleep function is designed specifically to stop (Return) when the wakeup function is called. If you don't want it to stop sleeping, then you'll simply have to redesign the sleep function slightly to accommodate that. Example?

Func mySleep($tt)
    Local $begin = TimerInit()
    
    While TimerDiff($begin) < $tt
        If $haltsleep Then
            $haltsleep = False
            consolewrite("going back to sleep" & @CRLF)
        EndIf
        Sleep(20)
        
    WEnd
EndFunc;==>mySleep
Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Share this post


Link to post
Share on other sites

I know Martin's code stops it, and I'm probably going to end up using it as it's exactly what I need. What I was wondering is when they said onevent and hotkeys interrupts the sleep does that mean it pauses it till whatever the event or hotkey called is done, and then goes back to sleep?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

(See my edit)

The only difference between either method is how the event is generated/captured. If you want to cancel the sleep through the tray icon or some GUI event, then OnEvent is the way to go. If you want to cancel by some keyboard press, then HotKeySet is the way to go.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Share this post


Link to post
Share on other sites

(See my edit)

The only difference between either method is how the event is generated/captured. If you want to cancel the sleep through the tray icon or some GUI event, then OnEvent is the way to go. If you want to cancel by some keyboard press, then HotKeySet is the way to go.

Ok you are not getting what I'm saying. Lets say I have a plain old ordinary sleep(x) and I press a hotkey sometime during that sleep. I know that pressing a hotkey or using onevent will stop the pause execute whatever the hotkey does. What I'm confused about is what then happens, does it continue the sleep now from where it left off, or does it just skip on to the next thing after the sleep.

I'm going to use Martin's code, it does what I want. I'm now trying to understand what the effect of using an event in the middle of a sleep() does?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

As for whether it'll cancel the sleep, or pause it, that depends on how you implement it. In martin's code, it'll stop, because the custom sleep function is designed specifically to stop (Return) when the wakeup function is called. If you don't want it to stop sleeping, then you'll simply have to redesign the sleep function slightly to accommodate that.

The only difference between either method [hotkey vs. OnEvent] is how the event is generated/captured. If you want to cancel the sleep through the tray icon or some GUI event, then OnEvent is the way to go. If you want to cancel by some keyboard press, then HotKeySet is the way to go.

I don't think you're reading my posts carefully enough. If martin's code works for you, then the OnEvent code will too. As I already said, the only difference between the two methods is how you tell the script to stop sleeping (global hotkey vs. some tray icon or GUI control action). Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Share this post


Link to post
Share on other sites

Ok you are not getting what I'm saying. Lets say I have a plain old ordinary sleep(x) and I press a hotkey sometime during that sleep. I know that pressing a hotkey or using onevent will stop the pause execute whatever the hotkey does. What I'm confused about is what then happens, does it continue the sleep now from where it left off, or does it just skip on to the next thing after the sleep.

Something like this?

HotKeySet("{f8}", "_foo")
Sleep(1000000000)
MsgBox(0, "", "End")

Func _foo()
  MsgBox(0, "", "Do Stuff")
EndFunc

If I remember correctly, it "continues the sleep", as you say. Just run the code to find out!

Share this post


Link to post
Share on other sites

Something like this?

HotKeySet("{f8}", "_foo")
Sleep(1000000000)
MsgBox(0, "", "End")

Func _foo()
  MsgBox(0, "", "Do Stuff")
EndFuncoÝ÷ ØÈ­é·«rëyËeÊ+jº¢{b笶¬ç©ªê-j̨ºÆ²&ë-®éíç(uëh~)Ý¢ëwßú®¢×ºyòr^";¬·
èµæÚ±çh*.®Æ®¶­sd÷D¶W6WBgV÷C·¶cÒgV÷C²ÂgV÷CµöföògV÷C²¢b33cµ6ÆVWÓS¢b33cµFÖW#ÕFÖW$æB¥6ÆVWb33cµ6ÆVW¤×6t&÷ÂgV÷C²gV÷C²ÂgV÷C´VæBgV÷C² ¤gVæ2öföò¢×6t&÷ÂgV÷C´çFW'WFær6ÆVWgV÷C²ÂgV÷C´æVVBFò6ÆVWf÷"æ÷FW"gV÷C²fײ7G&ærb33cµ6ÆVWÒ&÷VæBFÖW$Ffbb33cµFÖW"¤VæDgVæ0

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