Sign in to follow this  
Followers 0
Baritonomarchetto

How to minimize CPU usage

8 posts in this topic

Hi All,

I am trying to figure out how to reduce the CPU usage of a script like this:

#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")

While 1
If WinActive("[CLASS:MYWIN]") Then
   function()
EndIf
Sleep(80) ; keep the CPU cool
WEnd

Func function()
If _IsPressed("30", $hDLL)  Then
...
...
...
EndIf
EndFunc

It relies on key pressing so i suppose i cannot go high with "Sleep" (which, i suppose, could reduce the usage by reducing the number of cycles per second). Any idea?

Thanks :)

Share this post


Link to post
Share on other sites



Can you tell us what you try to achieve? Maybe there is a simpler solution.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I want that, while a defined program is active, my button-press related function is triggered... the target program can go unactive then active, and so on, so i suppose there are no better solutions other than set "WinActive" into the "while" loop, which probably is the heaviest part, isn't it?

Edited by Baritonomarchetto

Share this post


Link to post
Share on other sites

I would raise the sleep time from 80 milliseconds to at least 250 to keep the CPU cool. You would have to wait 1/4 second maximum. That should be acceptable.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks :)

Share this post


Link to post
Share on other sites

On a lot of scripts I do I turn GUIOnEventMode on and use Sleep(2100000000) instead of using a While loop, which is a good part of a month before the program will exit on its own. If you want it to run longer the same sleep time in a loop will work. The Gui can also be created and destroyed with this mode via a HotKey, making the code do almost nothing whatsoever except Sleep until you act on it. Even with the Gui active there is no active While loop polling the Gui.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Use diferent Sleep time when your window is/isn't active

While 1
If WinActive("[CLASS:MYWIN]") Then
function()
Sleep(25) ; keep the CPU cool
Else
Sleep(250) ; keep the CPU cool
EndIf
WEnd

Good idea!

Also, on larger projects with lots of windows, I find its good to nest your If statements for control clicks in IF statements that test if the message comes from a specific window. That way you avoid doing ALL the tests every iteration.

While 1
$msg = GUIGetMsg(1)

if $msg[0] == $win1 then

;...IF statements for testing controls on first window

elseif $msg[0] == $win2 then

;...Likewise for second window.

Endif
Wend
Edited by twitchyliquid64

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

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