Jump to content

How to minimize CPU usage


Recommended Posts

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 :)

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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.

Link to comment
Share on other sites

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

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...