Jump to content
Sign in to follow this  
Deathboy

Sleep question

Recommended Posts

Deathboy

Well the thing is I got a script witch gonna check something every 30min

So I put a sleep command in for 30min.

But if I wanna abort the waiting and do it manually I can't because the script won't respond because its sleeping

Any idea how to make it work?

Share this post


Link to post
Share on other sites
Thornhunt

Well the thing is I got a script witch gonna check something every 30min

So I put a sleep command in for 30min.

But if I wanna abort the waiting and do it manually I can't because the script won't respond because its sleeping

Any idea how to make it work?

general help and support

this belongs in that section i belive

and you could do this with hotkeys


Budweiser + room = warm beerwarm beer + fridge = too long!warm beer + CO2 fire extinguisher = Perfect![quote]Protect the easly offended ... BAN EVERYTHING[/quote]^^ hmm works for me :D

Share this post


Link to post
Share on other sites
JRowe

Pretty simple. Create a function called _Pause() that does something like this:

Func _Pause()
Local $a = TimerInit()
Local $b = 0
While $b < 30000 ; Or however long you want it to sleep
$b = TimerDiff($a)
Sleep(50) ; Sleeps in 50 ms increments, so the CPU doesn't get hogged
Wend

Share this post


Link to post
Share on other sites
Deathboy

Pretty simple. Create a function called _Pause() that does something like this:

Func _Pause()
Local $a = TimerInit()
Local $b = 0
While $b < 30000 ; Or however long you want it to sleep
$b = TimerDiff($a)
Sleep(50) ; Sleeps in 50 ms increments, so the CPU doesn't get hogged
Wend

Doesn't seem to work :/ still the same that it won't respond:/ But thanks for the reply :idea:

Share this post


Link to post
Share on other sites
JRowe

You'll have to add code that does something. Use HotKeySet() to call the function you want to execute manually. In the future, when you post help and support questions, please include your code (in [autoit] tags.) That way, we can help you more, because we can see exactly what the problems are. Hope this helps :idea:

Share this post


Link to post
Share on other sites
Deathboy

You'll have to add code that does something. Use HotKeySet() to call the function you want to execute manually. In the future, when you post help and support questions, please include your code (in [autoit] tags.) That way, we can help you more, because we can see exactly what the problems are. Hope this helps :idea:

okay, I'm a very shitty scripter:P

but took a little part from the code just to show what I mean.

#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
$mainwindow = GUICreate("Posten sporing", 250, 150)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUISetState(@SW_SHOW)   

$updatebutton = GUICtrlCreateButton("AutoCheck", 124, 110, 60, 25)
GUICtrlSetOnEvent($updatebutton, "autocheck")

Func autocheck()
    while 1
    sleep(5000)
    MsgBox("lol","hi", ":D", "")
    wend
EndFunc

Func Quit()
    Exit
EndFunc

while 1
    sleep(100000)
wend

If i push autocheck button in the GUI I can't close it with the X in top right corner. Can close it if I don't push the autocheck button. Your code didn't work either for it JRowe :/

Edited by Deathboy

Share this post


Link to post
Share on other sites
JRowe

While it's sleeping for 100000 ms, it does nothing. Instead of one gigantic sleep during which the script can't do anything, you want the script to test whether or not it should be doing something.

Sleep literally stops the script for however many ms you specify. Use TimerInit to set a timestamp and timerdiff to test how long its been since the timestamp, and use a smaller sleep (10-100 ms.) For example:

#include <GUIConstantsEx.au3>
Global $mainwindow = GUICreate("Posten sporing", 250, 150)
Global $updatebutton = GUICtrlCreateButton("AutoCheck", 124, 110, 60, 25)
GUISetState(@SW_SHOW)

Global $a = TimerInit()
Global $b = 0

While 1
    $b = TimerDiff($a)
    If $b > 3000 Then
        $a = TimerInit() ; reset the timer
        _AutoCheck()
    EndIf
    $msg = GUIGetMsg()
    If $msg = $updatebutton Then
        $a = TimerInit() ; reset the timer after manual activation
        _AutoCheck("Called manually from $updatebutton")
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then
        Quit()
    EndIf
    Sleep(10) ; small sleeps so it doesn't hog CPU, but remains responsive.
WEnd

Func _AutoCheck($s_Message = "This is an autocheck.")
    MsgBox(0, $s_Message, "Checking!")
EndFunc ;==>_AutoCheck

Func Quit()
    Exit
EndFunc ;==>Quit

That keeps it responsive. I'd recommend not using onevent mode for something like that. Also, use Select/Case if you're going to have more than one GUI event, so that the code is easier to understand and add to.

Share this post


Link to post
Share on other sites
Deathboy

While it's sleeping for 100000 ms, it does nothing. Instead of one gigantic sleep during which the script can't do anything, you want the script to test whether or not it should be doing something.

Sleep literally stops the script for however many ms you specify. Use TimerInit to set a timestamp and timerdiff to test how long its been since the timestamp, and use a smaller sleep (10-100 ms.) For example:

#include <GUIConstantsEx.au3>
Global $mainwindow = GUICreate("Posten sporing", 250, 150)
Global $updatebutton = GUICtrlCreateButton("AutoCheck", 124, 110, 60, 25)
GUISetState(@SW_SHOW)

Global $a = TimerInit()
Global $b = 0

While 1
    $b = TimerDiff($a)
    If $b > 3000 Then
        $a = TimerInit() ; reset the timer
        _AutoCheck()
    EndIf
    $msg = GUIGetMsg()
    If $msg = $updatebutton Then
        $a = TimerInit() ; reset the timer after manual activation
        _AutoCheck("Called manually from $updatebutton")
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then
        Quit()
    EndIf
    Sleep(10) ; small sleeps so it doesn't hog CPU, but remains responsive.
WEnd

Func _AutoCheck($s_Message = "This is an autocheck.")
    MsgBox(0, $s_Message, "Checking!")
EndFunc ;==>_AutoCheck

Func Quit()
    Exit
EndFunc ;==>Quit

That keeps it responsive. I'd recommend not using onevent mode for something like that. Also, use Select/Case if you're going to have more than one GUI event, so that the code is easier to understand and add to.

Sorry to nag again^^ but now it starts the autochecking as soon as I open the program.

Its not supposed to do so before the Autocheck button is pushed^^

Share this post


Link to post
Share on other sites
MHz

A change to the script by JRowe with autocheck using a flag variable to only use autocheck if the autocheck button has been clicked.

#include <GUIConstantsEx.au3>
Global $mainwindow = GUICreate("Posten sporing", 250, 150)
Global $updatebutton = GUICtrlCreateButton("AutoCheck", 124, 110, 60, 25)
GUISetState(@SW_SHOW)

Global $handle_timer ; handle returned from TimerInit()
Global $autocheck = False ; boolean value for AutoCheck button

While 1
    $msg = GUIGetMsg()
    If $msg = $updatebutton Then
        $autocheck = True ; activate autocheck
        $handle_timer = TimerInit() ; reset the timer
        _AutoCheck("Called manually from $updatebutton")
    ElseIf $msg = $GUI_EVENT_CLOSE Then
        Quit()
    EndIf
    If $autocheck Then
        If TimerDiff($handle_timer) > 5000 Then
            $handle_timer = TimerInit() ; reset the timer
            _AutoCheck()
        EndIf
    EndIf
WEnd

Func _AutoCheck($s_Message = "This is an autocheck.")
    MsgBox(0, $s_Message, "Checking!", 1)
EndFunc ;==>_AutoCheck

Func Quit()
    Exit
EndFunc ;==>Quit

You could also use AdlibRegister() if suitable.

Example of AdlibRegister

#include <GUIConstantsEx.au3>
Global $mainwindow = GUICreate("Posten sporing", 250, 150)
Global $updatebutton = GUICtrlCreateButton("AutoCheck", 124, 110, 60, 25)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg = $updatebutton Then
        AdlibRegister('_AutoCheck', 3000) ; activate autocheck
    ElseIf $msg = $GUI_EVENT_CLOSE Then
        Quit()
    EndIf
WEnd

Func _AutoCheck()
    MsgBox(0, @ScriptName, "Checking!", 1)
EndFunc

Func Quit()
    Exit
EndFunc

Share this post


Link to post
Share on other sites
Deathboy

A change to the script by JRowe with autocheck using a flag variable to only use autocheck if the autocheck button has been clicked.

#include <GUIConstantsEx.au3>
Global $mainwindow = GUICreate("Posten sporing", 250, 150)
Global $updatebutton = GUICtrlCreateButton("AutoCheck", 124, 110, 60, 25)
GUISetState(@SW_SHOW)

Global $handle_timer ; handle returned from TimerInit()
Global $autocheck = False ; boolean value for AutoCheck button

While 1
    $msg = GUIGetMsg()
    If $msg = $updatebutton Then
        $autocheck = True ; activate autocheck
        $handle_timer = TimerInit() ; reset the timer
        _AutoCheck("Called manually from $updatebutton")
    ElseIf $msg = $GUI_EVENT_CLOSE Then
        Quit()
    EndIf
    If $autocheck Then
        If TimerDiff($handle_timer) > 5000 Then
            $handle_timer = TimerInit() ; reset the timer
            _AutoCheck()
        EndIf
    EndIf
WEnd

Func _AutoCheck($s_Message = "This is an autocheck.")
    MsgBox(0, $s_Message, "Checking!", 1)
EndFunc ;==>_AutoCheck

Func Quit()
    Exit
EndFunc ;==>Quit

You could also use AdlibRegister() if suitable.

Example of AdlibRegister

#include <GUIConstantsEx.au3>
Global $mainwindow = GUICreate("Posten sporing", 250, 150)
Global $updatebutton = GUICtrlCreateButton("AutoCheck", 124, 110, 60, 25)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg = $updatebutton Then
        AdlibRegister('_AutoCheck', 3000) ; activate autocheck
    ElseIf $msg = $GUI_EVENT_CLOSE Then
        Quit()
    EndIf
WEnd

Func _AutoCheck()
    MsgBox(0, @ScriptName, "Checking!", 1)
EndFunc

Func Quit()
    Exit
EndFunc

Tnx alot MHz the first script you provided was just what I was looking for :idea:

Share this post


Link to post
Share on other sites
MHz

Your welcome.

I did minor changes to the good example script by JRowe so JRowe deserves most credit and thanks. Thanks JRowe. :idea:

Share this post


Link to post
Share on other sites
Deathboy

Your welcome.

I did minor changes to the good example script by JRowe so JRowe deserves most credit and thanks. Thanks JRowe. :idea:

Thanks JRowe too :) couln't have done it without you two :(

Share this post


Link to post
Share on other sites
JRowe

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.