Sign in to follow this  
Followers 0
Marcus

Using Sleep() to Overstress the CPU?

13 posts in this topic

Hello Folks!

Here's a snippet from the helpfile:

Basic MessageLoop Format

The general layout of MessageLoop code is:

While 1

$msg = GUIGetMsg()

...

...

WEnd

Usually a tight loop like the one shown would send the CPU to 100% - fortunately the GUIGetMsg function automatically idles the CPU when there are no events waiting. Do not put a manual sleep in the loop for fear of stressing the CPU . . .

How can we stress the CPU with Sleep() when we're essentially telling the system to do NOP for a set period of time?

Furthermore (if this is not a misprint), does this rule apply to all While loops???

Thanks --


[center][font="Courier"][quote]Which end do you put the cheesecake in?[/quote][/font][/center]

Share this post


Link to post
Share on other sites



Wow.... I would like to hear from a Dev on that one...

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Do not put a manual sleep in the loop for fear of stressing the CPU

This line needs to be read as: There is no need to worry about a stressed CPU and add a Sleep() to this While ... Wend loop. Its actually wrong to a Sleep() command to this Loop because you could loose messages generated by Control events....

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

@JdeB

THANK YOU!

This squares nicely with what I've observed in actual practice ;)

Didn't make sense when taken at face value . . .


[center][font="Courier"][quote]Which end do you put the cheesecake in?[/quote][/font][/center]

Share this post


Link to post
Share on other sites

This line needs to be read as: There is no need to worry about a stressed CPU and add a Sleep() to this While ... Wend loop. Its actually wrong to a Sleep() command to this Loop because you could loose messages generated by Control events....

just to understand

$msg = GUIGetMsg()....automatically idles the CPU when there are no events waiting

ok..but

when you use the option "Set on Event" ( no GUISetMsg() )

does the while/wend loop need a sleep?

thanks

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

when you use the option "Set on Event" ( no GUISetMsg() )

does the while/wend loop need a sleep?

thanks

8)

Yes correct, it will need a sleep() since there is nothing else to idle the script anymore ...

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

This line needs to be read as: There is no need to worry about a stressed CPU and add a Sleep() to this While ... Wend loop. Its actually wrong to a Sleep() command to this Loop because you could loose messages generated by Control events....

Ok, but the CPU cicles does not get release as it is stated in Help (This function automatically idles the CPU when required so that it can be safely used in tight loops without hogging all the CPU)

I mean, the CPU reaches 80-90% and then get down for a few seconds to 40-60% and then another jump to higher values... The computer reacts badly while there is no CPU available disregard what stated in help files. Besides this method (GuiGetMsg) I've tried GuiSetOnEvent but with same results.

What can I do?

I've searched the board but nothing conclusive, CPU and loop give me the same...

Here is a similar problem: http://www.autoitscript.com/forum/index.php?showtopic=9193

Share this post


Link to post
Share on other sites

Technical, this could have something to do with your code. If for instance you have something that must be performed on each iteration of the loop then (my understanding is that) AutoIt will have no choice but to constantly run the loop.

Would you be interested in posting some code that exhibits this weird CPU behaviour?

Share this post


Link to post
Share on other sites

Technical, this could have something to do with your code. If for instance you have something that must be performed on each iteration of the loop then (my understanding is that) AutoIt will have no choice but to constantly run the loop.

I understand that the help file says to not add Sleep command to avoid GUI non-responsive status but the continuous checking of the GUI using all CPU is prohibitive...

Would you be interested in posting some code that exhibits this weird CPU behaviour?

It needs more improvements. In fact, it would be an avast antivirus tweaker

(http://forum.avast.com/index.php?topic=16900.0)

B)

Share this post


Link to post
Share on other sites

The msgbox_messageloop.au3 example script (found under AutoIt\Examples\GUI\Simple) uses a loop to poll GUIGetMsg() without any Sleep(), however it uses 0% CPU. Here's the code for those that cannot be bothered hunting for it:

; A simple custom messagebox that uses the MessageLoop mode

#include <GUIConstants.au3>

GUICreate("Custom Msgbox", 210, 80)

$Label  = GUICtrlCreateLabel("Please click a button!", 10, 10)
$YesID  = GUICtrlCreateButton("Yes", 10, 50, 50, 20)
$NoID   = GUICtrlCreateButton("No", 80, 50, 50, 20)
$ExitID = GUICtrlCreateButton("Exit", 150, 50, 50, 20)

GUISetState() ; display the GUI

Do
    $msg = GUIGetMsg()
   
    Select
        Case $msg= $YesID
            MsgBox(0,"You clicked on", "Yes")
        Case $msg= $NoID
            MsgBox(0,"You clicked on", "No")
        Case $msg= $ExitID
            MsgBox(0,"You clicked on", "Exit")
        Case $msg= $GUI_EVENT_CLOSE
            MsgBox(0,"You clicked on", "Close")
    EndSelect
Until $msg = $GUI_EVENT_CLOSE or $msg = $ExitID

If your code follows this idea then you should not have a problem with excessive CPU usage.

Share this post


Link to post
Share on other sites

The msgbox_messageloop.au3 example script (found under AutoIt\Examples\GUI\Simple) uses a loop to poll GUIGetMsg() without any Sleep(), however it uses 0% CPU.

Many thanks... I'll test it.

First run only with this code give me un-responsive GUI...

But, of course, I need to test it into a 'long' code of my GUI and, specially, test in various conditions (stressed computer or not, etc.).

Thanks again

B)

Share this post


Link to post
Share on other sites

Thanks for the thread. I gave an autoit program that I run all the time and just tested it..

While 1

$i = MouseGetPos()

If _IsPressed('04') = 1 Then Call ("Calig")

If _IsPressed('02') = 1 AND $i[0] = @DesktopWidth -1 Then Call ("Show_Gui")

If _IsPressed('01') = 1 AND $i[0] = @DesktopWidth -1 Then Call ("Next_One")

If _IsPressed('01') = 1 AND $i[0] < @DesktopWidth -100 Then Call ("Hide")

$state = WinGetState("CPRS", "")

If Not BitAnd($state, 1) Then ProcessClose("AutoIt3.exe")

Sleep (10)

Wend

From Task Manager:

Without Sleep (10), CPU runs at 50%.

With Sleep (10), CPU is at 0%

Share this post


Link to post
Share on other sites

fmen, your code would be expected to stress the CPU without a Sleep() instruction because it does not use GUIGetMsg().

In fact, even if this loop were used for GUI message processing, it would not idle the CPU because _IsPressed() is used within it. A GUI processing loop should only contain what it needs to handle GUI events and nothing more, or else CPU usage will suffer.

P.S. Your code can also be written this way with the same result:

While 1
    $I = MouseGetPos()
    If _IsPressed('04') Then Calig()
    If _IsPressed('02') And $I[0] = @DesktopWidth - 1 Then Show_GUI()
    If _IsPressed('01') And $I[0] = @DesktopWidth - 1 Then Next_One()
    If _IsPressed('01') And $I[0] < @DesktopWidth - 100 Then Hide()
    If Not(WinExists('CPRS')) Then ProcessClose('AutoIt3.exe')
    Sleep(10)
WEnd

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