Icarus Posted March 5, 2009 Share Posted March 5, 2009 Hello! The code below is using 10-12% processor utilisation, and uses more and more depending on the number of windows open. Is there a more efficient way of detecting a new window without checking each one in turn every time? AIM : - If {there's a new window that we haven't seen before} then {do something} - without using loads of cycles to find out!! Cheers gurus - you rock!! Icarus CODEWhile 1 ; Get the list of Window titles $avWinListCurrent = WinList() For $n = $avWinListCurrent[0][0] To 1 Step -1 ; Check has title and visible If ($avWinListCurrent[$n][0] <> "") And BitAND(WinGetState($avWinListCurrent[$n][1]), 2) Then ; Have we seen this window before? $fFound = False For $i = 1 To $avWinListPrevious[0][0] If $avWinListCurrent[$n][1] = $avWinListPrevious[$i][1] Then $fFound = True ExitLoop EndIf Next ; No we haven't - its a new Window, let's do something with it..... If Not $fFound Then ;{do something...blah blah } Link to comment Share on other sites More sharing options...
BrettF Posted March 5, 2009 Share Posted March 5, 2009 Why not something like this: $oldList = WinList () While 1 $newList = WinList () If $oldList[0][0] <> $newList[0][0] Then MsgBox (0, "", "There is a new window!") $oldList = $newList EndIf WEnd Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Triblade Posted March 5, 2009 Share Posted March 5, 2009 You could just simply add a Sleep(100) before Next. Then the check would be done every 100 milliseconds instead of as fast as it can. My active project(s): A-maze-ing generator (generates a maze) My archived project(s): Pong3 (Multi-pinger) Link to comment Share on other sites More sharing options...
Icarus Posted March 5, 2009 Author Share Posted March 5, 2009 Thanks for the quick response!! I've tried the SLEEP (nn) in various places, but what happens then is that I miss some windows. As I'm trying to catch error messages, they may only be on screen for a couple of seconds... Brett - I'll try and incorporate your idea - nice one - and report back... Any other suggestions..?? Is this the best way?? Link to comment Share on other sites More sharing options...
weirddave Posted March 5, 2009 Share Posted March 5, 2009 A Sleep (100) before just before Wend should work. Assuming your code isn't taking ages, it should rattle round and check 10 times a second, so unless these windows are open for less than 100ms, you should spot them. Link to comment Share on other sites More sharing options...
Icarus Posted March 5, 2009 Author Share Posted March 5, 2009 A Sleep (100) before the WEND causes processor utilisation of ~50% on a 20-window session.... Its important to get the balance of checking (almost) all the time quickly, whilst not using much CPU, hence the need for a quick way to check for new windows. Off to try Brett's suggestion.. Much appreciated.. Link to comment Share on other sites More sharing options...
Icarus Posted March 5, 2009 Author Share Posted March 5, 2009 Using both suggestions (global array check and sleep timer before the WEND) the code now runs at ZERO %!!!It peaks at 1.5% CPU for one second when it detects a new Window, then goes back to zero %.When it does the work it needs (logging etc) it peaks a little higher, but essentially its fixed!!!I did say you guru guys are awesome...!!Love it!Love the app, love coding, and love these forums!!!!Thank you a million times over people..!!Icarus Link to comment Share on other sites More sharing options...
Triblade Posted March 5, 2009 Share Posted March 5, 2009 Great dude Glad it works for ya. My active project(s): A-maze-ing generator (generates a maze) My archived project(s): Pong3 (Multi-pinger) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now