Kiai Posted July 19, 2009 Share Posted July 19, 2009 I have created an application that waits for a certain window to be opened, and when it is, presents a small GUI in the title bar (height of the title bar, contains 4 buttons). I have also set it to move if the user changes the window position. The app loops, sleeps for 1 sec, deletes the GUI if the target window is no longer active. It all works, but when I check this out with Microsoft process explorer, it seems that this code runs the CPU a bit more than I expected -- nothing dramatic but it made an app that was using only 0.7% hit 2.5% at times. Any suggestions on making this more efficient? Thanks.$size = WinGetPos($title)If WinActive($title) And @error<>1 Then; if the size check fails because the window has been closed, @error is set to 1 $Open_chart_window = GUICreate("GUI_Open_chart",260, 16, $size[2]+$size[0]-340, $size[1]+4,$WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)......While WinActive($title) or WinActive("GUI_Open_chart") ;Keeps the GUI active as long as the main window is open $size_new=WinGetPos($title) If @error<>1 Then If $size_new[0]<>$size[0] Or $size_new[1]<>$size[1] Or $size_new[2]<>$size[2] Or $size_new[3]<>$size[3] Then WinMove("GUI_Open_chart","",$size_new[2]+$size_new[0]-340, $size_new[1]+4) WinActivate($title) $size=WinGetPos($title) EndIf EndIf Sleep(1000)WEnd GUIDelete("GUI_Open_chart") ;close the GUI when the main window is no longer active Link to comment Share on other sites More sharing options...
Beege Posted July 19, 2009 Share Posted July 19, 2009 Are you saying this script caused a different process to use more cpu than normal, or this script uses more cpu than you thought it would? Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
Kiai Posted July 19, 2009 Author Share Posted July 19, 2009 Are you saying this script caused a different process to use more cpu than normal, or this script uses more cpu than you thought it would?That wasn't very clear was it. This loop seems to take twice or more cpu% than a very similar loop in the same app that doesn't have the getwinpos statement. My question boils down to, is there a better way to essentially attach a gui to another window and have the gui travel with that window if moved, max'd or min'd? Link to comment Share on other sites More sharing options...
MDCT Posted July 20, 2009 Share Posted July 20, 2009 (edited) That wasn't very clear was it. This loop seems to take twice or more cpu% than a very similar loop in the same app that doesn't have the getwinpos statement. My question boils down to, is there a better way to essentially attach a gui to another window and have the gui travel with that window if moved, max'd or min'd?Not sure, but maybe CPU can be decreased if you add sleep after each CPU intensive command, in this case getwinpos.I'm assuming if you add sleep in last line after many commands, the CPU would need to process many commands before going to sleep.. so CPU is noticed higher.But, if you add smaller sleep after each command, it could make the CPU to go unnoticed.I'm not sure, and I don't know if it can help to decrease CPU or not. But, would like to know.Also, in your script, I think "If @error<>1 Then" is not necessary as the chance of getting error on wingetpos while the window is active is quite rare. I read If is quite high on CPU so reducing it would be better.One more thing, alternatively, if you are using it on AutoIt GUIs, you could register message, so you could intercept window move. Using: GUIRegisterMsg($WM_WINDOWPOSCHANGED, "WM_WINDOWPOSCHANGED") Edited July 20, 2009 by MDCT Link to comment Share on other sites More sharing options...
martin Posted July 20, 2009 Share Posted July 20, 2009 I have created an application that waits for a certain window to be opened, and when it is, presents a small GUI in the title bar (height of the title bar, contains 4 buttons). I have also set it to move if the user changes the window position. The app loops, sleeps for 1 sec, deletes the GUI if the target window is no longer active. It all works, but when I check this out with Microsoft process explorer, it seems that this code runs the CPU a bit more than I expected -- nothing dramatic but it made an app that was using only 0.7% hit 2.5% at times. Any suggestions on making this more efficient? Thanks. $size = WinGetPos($title) If WinActive($title) And @error<>1 Then; if the size check fails because the window has been closed, @error is set to 1 $Open_chart_window = GUICreate("GUI_Open_chart",260, 16, $size[2]+$size[0]-340, $size[1]+4,$WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW) ...... While WinActive($title) or WinActive("GUI_Open_chart") ;Keeps the GUI active as long as the main window is open $size_new=WinGetPos($title) If @error<>1 Then If $size_new[0]<>$size[0] Or $size_new[1]<>$size[1] Or $size_new[2]<>$size[2] Or $size_new[3]<>$size[3] Then WinMove("GUI_Open_chart","",$size_new[2]+$size_new[0]-340, $size_new[1]+4) WinActivate($title) $size=WinGetPos($title) EndIf EndIf Sleep(1000) WEnd GUIDelete("GUI_Open_chart") ;close the GUI when the main window is no longer active Not directly related to your question but you might have problems using WinGetPos when the application is minimized. If so using _WINAPI_GETWINDOWRECT might help which, as far as I have tried it, returns the correct coordinates for minimzed and normal windows. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
MDCT Posted July 20, 2009 Share Posted July 20, 2009 using WinGetPos when the application is minimized. Can a window be active and minimized at the same time? I'm not using explorer shell, so the I'm not sure.In that case my suggestion was wrong, the @error check is quite necessary. Link to comment Share on other sites More sharing options...
martin Posted July 20, 2009 Share Posted July 20, 2009 Can a window be active and minimized at the same time?No. But I thought that the OP might want to keep track of wether the app was minimized or not and then place some indicator near it or above it instead of closing his gui each time. Possibly an incorrect guess though. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. 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