Sign in to follow this  
Followers 0
nago

most efficient way to idle a script

7 posts in this topic

I'm sure this is in an FAQ somewhere, but I'm just not coming up with any luck at the moment.

I have a simple script that consists entirely of a HotKeySet and a UserFunction. I intend for it to *always* run in the background for windows, and it has no tray icon, etc.

What's the most efficient loop to keep it open? Currently I'm using something like:

While 1
Sleep (10000)
WEnd

Which seems like it can't be the most efficient way to idle a script. suggestions?

Share this post


Link to post
Share on other sites



I'm sure this is in an FAQ somewhere, but I'm just not coming up with any luck at the moment.

I have a simple script that consists entirely of a HotKeySet and a UserFunction. I intend for it to *always* run in the background for windows, and it has no tray icon, etc.

What's the most efficient loop to keep it open? Currently I'm using something like:

While 1
Sleep (10000)
WEnd

Which seems like it can't be the most efficient way to idle a script. suggestions?

How does a script sleep "efficiently"? :)

What you have should work fine, what don't you like about it?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

It just seemed like kind of a "dirty" way to do it, I guess. Because it still needs to wake up every 10000 units to figure out what it should do next, which will be sleep again...

In my case, I define efficiency as least CPU cycles/memory used. I was just wondering if there was a built-in "idle" or "pause" function or something similar that didn't require a sort of hackish loop, but if this is the best way to do it, that's fine by me.

It doesn't really bother me that much, I was just asking for best practices :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I define efficiency as least CPU cycles/memory used

Sleep makes it so your thread does not use it's share of processor time. So instead of CPU cycles being use on your thread, the OS gets to give that time to other threads. In that sense, sleep(0) would be more 'efficient'. But that also means that your application is re-polled more often, which also uses cycles, so in that sense Sleep(100), Sleep(1000), or Sleep(10000) are more 'efficient'.

Well that's my understanding of it anyway...(maybe not entirely true though)...however in practice I've never really had to worry about it much. I usually use Sleep(100) myself.

Edited by evilertoaster

Share this post


Link to post
Share on other sites

It just seemed like kind of a "dirty" way to do it, I guess. Because it still needs to wake up every 10000 units to figure out what it should do next, which will be sleep again...

In my case, I define efficiency as least CPU cycles/memory used. I was just wondering if there was a built-in "idle" or "pause" function or something similar that didn't require a sort of hackish loop, but if this is the best way to do it, that's fine by me.

It doesn't really bother me that much, I was just asking for best practices :)

You're seriously underestimating your computer. It could repeat that loop thousands of times each second and still not really hogging it.

Also, you should know that this is in no way a dirty way. All windows apps uses loops to keep going.

:)


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Maybe what your looking for:

#include <misc.au3>

Global $x=false

HotKeySet("{F5}","YourScript")
HotKeySet("{ESC}","ExitNow")

While 1
    Sleep(50)
Wend

Func ExitNow()
    MsgBox(0, "Hey there noob", "Cya, wouldn't wanna be ya!")
    Exit
EndFunc


Func YourScript()
    If $x=true Then
        $x=False
    Elseif $x=false Then
        $x=true
    Endif
    While $x=true
    ;DO YOUR CODING HERE
    WEnd
EndFunc

A quick explanation...

$x is set to false from the beginning Global at the top right? So when you Press F5 to begin Func YourScript() it goes to the ElseIf statement because $x=false... After it reads that, it changes $x to true (right below the ElseIf)

Now your While loop will only go while $x=true (which it is)... Now to pause/stop the script you would press F5 again to enter Func YourScript() again... But this time $x=true... So it starts at the If statement and changes $x to false... Now the While statement (While $x=true) is no longer true, so it will not run...

Hope I made that as clear as possible :S

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