Steven_H Posted December 16, 2021 Posted December 16, 2021 Hello I'm new to AutoIt, but impressed so far with how well it works to solve an ongoing problem I have had. Firstly my script below does work and it does do what I need it to, but I'm sure it is very badly written and consumes 25% CPU. I'm hoping someone can point out how it could be improved or done in a much better way. #include <FileConstants.au3> ; Create file in same folder as script $sFileName = "c:\Log\Amaya_Log.txt" $sID = "" $oLd = "" Run("AMAYA.exe") Func getAmaya() While 1 if WinExists("AMAYA OS Lite") Then $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]") If ($sID == $oLD) Then ContinueLoop Else ConsoleWrite($sID & @CRLF) ; Append a line Local $hFileOpen = FileOpen($sFileName, $FO_APPEND) FileWrite($hFileOpen, @CRLF & $sID) FileClose($hFileOpen) $oLd = $sID EndIf Sleep (50) EndIf WEnd EndFunc getAmaya() What I am doing here is reading a control from an old Embroidery Machine program which does not save external logs, it only shows these inside the program and once it's closed they are lost. In the attached image the Control I am capturing is at the bottom left of the screen which has a dropdown box, currently showing "20:22:30 - Cluster 01 - Head 01 - Design has completed sewing." The script constantly Loops checking if this value changes and if it does, writes it to a text file. I was hoping there was some sort of OnChange or EnChange event I could use, but as this is an external Program not a GUI I've created in AutoIt, I couldn't find a less resource intensive way to achieve this. I will probably also log several other controls from the program as well, but initially just want to get this part running more efficiently and learn from others in the forums. Please don't be too harsh, but I'm aware this is probably the total wrong way to achieve this. Thanks in advance
Solution Zedna Posted December 16, 2021 Solution Posted December 16, 2021 fix: Sleep(50) ; --> added by Zedna #include <FileConstants.au3> ; Create file in same folder as script $sFileName = "c:\Log\Amaya_Log.txt" $sID = "" $oLd = "" Run("AMAYA.exe") Func getAmaya() While 1 If WinExists("AMAYA OS Lite") Then $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]") If ($sID == $oLd) Then Sleep(50) ; --> added by Zedna ContinueLoop Else ConsoleWrite($sID & @CRLF) ; Append a line Local $hFileOpen = FileOpen($sFileName, $FO_APPEND) FileWrite($hFileOpen, @CRLF & $sID) FileClose($hFileOpen) $oLd = $sID EndIf Sleep(50) EndIf WEnd EndFunc ;==>getAmaya getAmaya() Resources UDF ResourcesEx UDF AutoIt Forum Search
Subz Posted December 16, 2021 Posted December 16, 2021 You only need to open the file once at the top of the script and as Zedna mentioned move the Sleep function although I would have placed it outside of the If statement for example (untested): #include <FileConstants.au3> ;~ Set a hotkey Escape to exit the script HotKeySet("{ESC}", "_ExitScript") ; Create file in same folder as script Global $sFileName = "c:\Log\Amaya_Log.txt" Global $hFileOpen = FileOpen($sFileName, $FO_APPEND) Run("AMAYA.exe") getAmaya() Func getAmaya() Local $sID = "", $oLd = "" While 1 If WinExists("AMAYA OS Lite") Then $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]") If ($sID == $oLD) Then ContinueLoop Else ConsoleWrite($sID & @CRLF) ; Append a line FileWrite($hFileOpen, $sID & @CRLF) $oLd = $sID EndIf EndIf Sleep (50) WEnd EndFunc Func _ExitScript() FileClose($hFileOpen) Exit EndFunc
Steven_H Posted December 16, 2021 Author Posted December 16, 2021 Thanks to you both, will give this a try once there is a break in the machines use. Looks like I wasn't a millions miles away, I had the Sleep (50) in the wrong place and not specifying Local/Global. I was going to to look at a way to exit the script without having to exit it from the System Tray, saved me searching @Subz.
Zedna Posted December 16, 2021 Posted December 16, 2021 Sleep must be also before ContinueLoop (as in my script) to reduce CPU use when there is no change in text got from editbox. Resources UDF ResourcesEx UDF AutoIt Forum Search
Nine Posted December 17, 2021 Posted December 17, 2021 Or just put the sleep at the very beginning of the loop, so in all cases it will be executed “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy
Steven_H Posted December 27, 2021 Author Posted December 27, 2021 Sorry for not replying and confirming the solution sooner. I marked @Zedna as the solution because it solved the problem of the high CPU usage. Although I combined it with @Subzas well. The current version I am now using is this: #include <FileConstants.au3> ;~ Set a hotkey Escape to exit the script HotKeySet("{ESC}", "_ExitScript") ; Create file in same folder as script Global $sFileName = "c:\Log\Amaya_Log.txt" Global $hFileOpen = FileOpen($sFileName, $FO_APPEND) Run("AMAYA.exe") getAmaya() Func getAmaya() Local $sID = "", $oLd = "" While 1 If WinExists("AMAYA OS Lite") Then $sID = ControlGetText("AMAYA OS Lite", "", "[CLASS:Edit; INSTANCE:22]") If ($sID == $oLD) Then Sleep(50) ContinueLoop Else ConsoleWrite($sID & @CRLF) ; Append a line FileWrite($hFileOpen, $sID & @CRLF) $oLd = $sID EndIf EndIf Sleep (50) WEnd EndFunc Func _ExitScript() FileClose($hFileOpen) Exit EndFunc Currently working on slightly more complicated version which is pulling multiple controls and trying to then submit as Json to an MQTT broker, but will post that as its own post. Thanks for the help.
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