Sign in to follow this  
Followers 0
czardas

Is there any advantage to not using Sleep

7 posts in this topic

#1 ·  Posted (edited)

Everyone uses their favourite functions for different reasons. I tend to avoid using sleep, however I sometimes need a function to pause my script.

In this case I am setting the sensitivity of a custom designed graphic up/down control. I have a label which contains several numbers. When the user clicks the up button, the label is updated instantly and all the numbers change at once. If the left mouse button is held down, after a short delay the label begins to scroll through the numbers at a faster pace.

To create the delay before the scrolling kicks in, I can either use Sleep or TimerDiff. So what is the difference between the two methods. Is there any advantage of using one over the other?

; Method 1
Sleep(250)

; Method 2
$lastScroll = TimerInit()
While TimerDiff($lastScroll) < 250
WEnd

Edit

I think I'll use TimerDiff, just in case I decide to put something inside the loop. I don't like the script being unresponsive for a fraction of a second. Still I thought it would be worth while asking your opinion.

Edited by czardas

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think that becuase of this post that I am going to stop using sleep too. I don't like the unreponsiveness sometimes.

But wait, if you have a while loop without a sleep then won't the CPU get bogged down? I mean not for 250 mseconds, but oh wait, no.

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

A practical example, if you want to update the content of a label at every 20 s [for example] and don't want to use AdlibRegister().

$MAIN = GUICreate("Example",300,220)
$LABEL = GUICtrlCreateLabel("",100,100,100,20,0x1001)
GUICtrlSetFont($LABEL,12,600,0,"Aria Black")
GUISetState(@SW_SHOW,$MAIN)

While True
    UpdateControl($LABEL) ;Update the label with some random chars every 20 s
WEnd

Func UpdateControl($LABEL)
    Local $RND_CHARS
    For $I = 1 To 5
        $RND_CHARS &= Chr(Random(65,90,1))
    Next
    GUICtrlSetData($LABEL,$RND_CHARS)
    ;Ex 1 - good when you don't need to do something else then wait
    Sleep(20000) ;Script will be freeze for 20 s
    ;Ex 2 - good when you have to catch some GUI messages or other things to do
    $T_INIT = TimerInit()
    While TimerDiff($T_INIT)/1000 < 20
        If GUIGetMsg() = -3 Then Exit
    WEnd
EndFunc

Edit: use just Ex 1 or Ex 2, not both same time :)

Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Hmm, what if some idiot user keeps clicking the control instead of just holding down the left the mouse key. If the script uses sleep, nothing can happen during that time. I keep thinking I may have to change tak and use something like GUIRegisterMsg. The trouble with all these different options is that I get confused. I never know which one to choose.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here's the control. It is still in the design phase, so the code is not so clean. The Up and Down functions will be combined when I am am happy with it.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#Include <Misc.au3>

; Create GUI
Local $hGUI = GUICreate("Scroll Label", 130, 230)
GUISetBkColor(0xFFFFFF, $hGUI)

$hUp = GUICtrlCreateGraphic(13, 80, 31, 31)
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetColor(-1, 0x8096DC)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xBBBBBB, 0xBBBBBB)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 15, 7)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 26,24)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 4,24)

$hDown = GUICtrlCreateGraphic(13, 117, 31, 31)
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetColor(-1, 0x8096DC)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xBBBBBB, 0xBBBBBB)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 15, 24)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 25, 6)
GUICtrlSetGraphic(-1, $GUI_GR_LINE, 4, 6)

$iPos = 0
Local $asSlider[12] = ["1"&@LF&@LF&"2"&@LF&@LF&"3"&@LF&@LF&"4"&@LF&@LF&"5","2"&@LF&@LF&"3"&@LF&@LF&"4"&@LF&@LF&"5"&@LF&@LF&"6", _
"3"&@LF&@LF&"4"&@LF&@LF&"5"&@LF&@LF&"6"&@LF&@LF&"7","4"&@LF&@LF&"5"&@LF&@LF&"6"&@LF&@LF&"7"&@LF&@LF&"8", _
"5"&@LF&@LF&"6"&@LF&@LF&"7"&@LF&@LF&"8"&@LF&@LF&"9","6"&@LF&@LF&"7"&@LF&@LF&"8"&@LF&@LF&"9"&@LF&@LF&"10", _
"7"&@LF&@LF&"8"&@LF&@LF&"9"&@LF&@LF&"10"&@LF&@LF&"11","8"&@LF&@LF&"9"&@LF&@LF&"10"&@LF&@LF&"11"&@LF&@LF&"12", _
"9"&@LF&@LF&"10"&@LF&@LF&"11"&@LF&@LF&"12"&@LF&@LF&"13","10"&@LF&@LF&"11"&@LF&@LF&"12"&@LF&@LF&"13"&@LF&@LF&"14", _
"11"&@LF&@LF&"12"&@LF&@LF&"13"&@LF&@LF&"14"&@LF&@LF&"15","12"&@LF&@LF&"13"&@LF&@LF&"14"&@LF&@LF&"15"&@LF&@LF&"16"]

$hSliderPos = GUICtrlCreateLabel($asSlider[0], 57, 47, 15, 140, $SS_CENTER)
;GUICtrlSetBkColor(-1, 0xEEEEEE)
GUICtrlSetFont(-1, 9, 400, -1, "Arial")
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

    If $msg = $hUp Then
        _Up($iPos)
    EndIf

    If $msg = $hDown Then
        _Down($iPos)
    EndIf
WEnd

Func _Up(ByRef $iPos)
    If $iPos < 11 Then
        $iPos += 1
        GUICtrlSetData($hSliderPos , $asSlider[$iPos])
    EndIf
    $lastScroll = TimerInit()
    While TimerDiff($lastScroll) < 200
        If Not _IsPressed("01") Then Return
    WEnd
    While _IsPressed("01")
        If $iPos < 11 And TimerDiff($lastScroll) > 100 Then
            $iPos += 1
            $lastScroll = TimerInit()
            GUICtrlSetData($hSliderPos, $asSlider[$iPos])
        EndIf
    WEnd
EndFunc

Func _Down(ByRef $iPos)
    If $iPos > 0 Then
        $iPos -= 1
        GUICtrlSetData($hSliderPos , $asSlider[$iPos])
    EndIf
    $lastScroll = TimerInit()
    While TimerDiff($lastScroll) < 200
        If Not _IsPressed("01") Then Return
    WEnd
    While _IsPressed("01")
        If $iPos > 0 And TimerDiff($lastScroll) > 100 Then
            $iPos -= 1
            $lastScroll = TimerInit()
            GUICtrlSetData($hSliderPos , $asSlider[$iPos])
        EndIf
    WEnd
EndFunc

It all works fine until you double click (or more). It doesn't always respond to a double click. I guess the responsiveness of the mouse may also be a factor. Perhaps I shouldn't worry about it so much. Holding the left mouse button scolls, but you have to wait a fraction of a second before you can click again.

It seems I found something to put in my while loop.

While TimerDiff($lastScroll) < 200
    If Not _IsPressed("01") Then Return ; Added
WEnd

It does make a difference, although it's hard to tell. :)

I suppose it's not so bad the way it is.

Edited by czardas

Share this post


Link to post
Share on other sites

You could do it this way to avoid that double-click thing:

Global $aInfo
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $GUI_EVENT_PRIMARYDOWN Then
        $aInfo = GUIGetCursorInfo()
        If $aInfo[4] = $hUp Then _Up($iPos)
        If $aInfo[4] = $hDown Then _Down($iPos)
    EndIf
;~  If $msg = $hUp Then
;~         _Up($iPos)
;~     EndIf

;~     If $msg = $hDown Then
;~         _Down($iPos)
;~     EndIf
WEnd

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

You could do it this way to avoid that double-click thing:

Trancexx, that's great. you are so clever. Thank you.

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