angel83 Posted 21 hours ago Posted 21 hours ago Hi, I'm working on a script that closes running processes. The problem I'm having is that it consumes CPU resources. I've tried changing sleep(10) to sleep(5000), and the CPU usage does indeed decrease, but it takes a while to close the process. What do you think I could do? if there is another way to do it I appreciate your help. This is the code I'm working on: #include <MsgBoxConstants.au3> #include <GUIConstants.au3> #include <File.au3> HotKeySet("{9}", "goout") While 1 Sleep (10) Local $aArray = FileReadToArray(@ScriptDir & "\test.dat") Local $iLineCount = @extended For $i = 0 To $iLineCount - 1 If ProcessExists($aArray[$i]) Then ProcessClose($aArray[$i]) EndIf Next Wend Func goout() Exit EndFunc ;==>Terminate This is what I have inside test.dat as an example:test.dat notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe notepad.exe
ioa747 Posted 20 hours ago Posted 20 hours ago #include <MsgBoxConstants.au3> #include <GUIConstants.au3> #include <File.au3> HotKeySet("{9}", "goout") Local $sTestData While 1 Local $sTestNewData = FileRead(@ScriptDir & "\test.dat") If $sTestData <> $sTestNewData Then $sTestData = $sTestNewData Local $aData = StringSplit($sTestData, @CRLF, 1) For $i = 0 To $aData[0] If ProcessExists($aData[$i]) Then ConsoleWrite($aData[$i] & " ProcessClose: " & ProcessClose($aData[$i]) & @CRLF) Next EndIf Sleep(50) WEnd Func goout() Exit EndFunc ;==>goout I know that I know nothing
AspirinJunkie Posted 20 hours ago Posted 20 hours ago 1 hour ago, angel83 said: What do you think I could do? Describe what the real end goal is. For an outside observer, this raises a few questions: Why do you need a file with process names? Why do you keep re-reading the file? - Does the file change during script execution? If so, are only new lines added, or is anything deleted? Why do the process names appear multiple times in the file? Do you only need to react when the file is changed, or permanently? Once the real goal is known, you can optimize in a completely different way. (Keep the >x-y problem< in mind here)
angel83 Posted 19 hours ago Author Posted 19 hours ago First of all, thank you for responding. Thanks AspirinJunkie . I don't think I gave too much information. Here are the answers to your questions. Why do you need a file with process names? = Because another script added them to the test.dat file. Why do you keep rereading the file? - Does the file change during script execution? If so, are new lines added or removed? = The test.dat file contains the names of the executables I want to close. Why do process names appear multiple times in the file? = This was just an example, as names can be different, for example: chrome.exe photoshop.exe GIMP.exe CCleaner.exe Visual Studio .exe Do you only need to react when the file is changed, or permanently? The executable names will be permanently stored in the test.dat file, so the process can close them.
AspirinJunkie Posted 18 hours ago Posted 18 hours ago 24 minutes ago, angel83 said: The test.dat file contains the names of the [...] The executable names will be permanently stored in the test.dat file, so the process can close them. So that means the file doesn't change during script execution? Then you don't need to read it in over and over again, just once: before the while loop. That should largely solve your performance issues. But change your sleep to a higher value to reduce the load on the CPU (e.g., 250 or something like that).
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