Jump to content

Question about the While loop idle


MattH
 Share

Recommended Posts

Just curious. Why is it suggested to use

While 1

Sleep(1000) ; Idle around

WEnd

instead of

Sleep(2147483647)

for event driven scripts?

The memory used by the loop is about three times than that of just sleep. Is there another drawback to using sleep other than the program will terminate in 24 days (at the max value)?

Thanks for your help.

Link to comment
Share on other sites

Just curious. Why is it suggested to use

While 1

Sleep(1000) ; Idle around

WEnd

instead of

Sleep(2147483647)

for event driven scripts?

The memory used by the loop is about three times than that of just sleep. Is there another drawback to using sleep other than the program will terminate in 24 days (at the max value)?

Thanks for your help.

In general people want a script to run untill requested to close, so a fixed sleep just to save a few bytes of memory would not be a great idea.

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.
Link to comment
Share on other sites

Also you might consider the fact that during a Sleep - the script does nothing - it will be practically "frozen" for the amount of time specified - you can't even close it during sleep time.

Hope it is a reason good enough :)

Test this and you'll see the point in what I said:

#include <GUIConstants.au3>

$Form1 = GUICreate("AForm1", 468, 128, 193, 115)
$Button1 = GUICtrlCreateButton("AButton1", 104, 40, 273, 41, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Sleep(20000)
    EndSwitch
WEnd

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Also you might consider the fact that during a Sleep - the script does nothing - it will be practically "frozen" for the amount of time specified - you can't even close it during sleep time.

Hope it is a reason good enough :)

Test this and you'll see the point in what I said:

#include <GUIConstants.au3>

$Form1 = GUICreate("AForm1", 468, 128, 193, 115)
$Button1 = GUICtrlCreateButton("AButton1", 104, 40, 273, 41, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Sleep(20000)
    EndSwitch
WEnd

But MattH did say he was talking about onevent mode, so if you put a long sleep inside the while loop, or just had a sleep as MattH suggested then you would still get a respone to events.

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.
Link to comment
Share on other sites

But this way, you don't need tight while loop... :)

#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("AForm1", 468, 128, 193, 115)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSE")
GUICtrlCreateButton("AButton1", 104, 40, 273, 41, 0)
GUICtrlSetOnEvent(-1, "button1")
GUISetState(@SW_SHOW)

While 1
    Sleep(2147483647)
WEnd

Func button1()
    MsgBox(0, "AForm1", "Ouch! You press the button")
EndFunc   ;==>button1

Func CLOSE()
    Exit
EndFunc   ;==>CLOSE
Link to comment
Share on other sites

The script still wouldn't close though, I think. Not if it was sleeping.

You should test it to see if what you think is correct.

enaiman might like to know the result.

Edited by martin
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.
Link to comment
Share on other sites

HotKeys will always work when the script is put to sleep :P

And about the Sleep inside the While loop I have to say that I'm a bit confused myself. My idea about the main loop was that the loop is executed over and over (and this is the behaviour of a While loop) when the control is passed back to it - script is idling. So it looks logical to me that once in the While loop the script should execute the instructions between.

if the example is:

#include <GUIConstants.au3>
AutoItSetOption ("TrayIconDebug", 1)
Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("AForm1", 327, 70, 193, 115)
GUISetOnEvent($GUI_EVENT_CLOSE, "AForm1Close")
$i=1
$Label1 = GUICtrlCreateLabel($i, 152, 16, 20, 17)
GUISetState(@SW_SHOW)

While 1
    Sleep(1000)
    $i +=1
    GUICtrlSetData($Label1, $i)
WEnd
        
Func AForm1Close()
    Exit
EndFunc

You can see that indeed the instructions are executed over and over and if you put a very long sleep then the other actions inside the while loop are delayed accordingly = this is the reason for using small Sleep times. Indeed the scipt answers to events if you put a long sleep between but you won't be able to do something inside the while loop. This way (small sleep) I consider to be more "appropriate" for learning and also a good practice (something like eliminating "go to" command).

And to tease a little ... change the Sleep inside my loop to 2147483647 and let me know when it will change to next value :) ... I could wait 25 days for that :)

... just a little joke :P

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...