Muxson Posted December 26, 2016 Share Posted December 26, 2016 Hi there, I'm posting for the first time but have been reading and learning a great deal ever since I started using AutoIt some years ago, so BIG THANKS for all the great inputs, guys! Here is my challenge to which I didn't find answers in other posts. It is specific to Running DOS command, or actually stopping cmd.exe when called with /c. To give you the background, I'm launching processing of audio files. If the user wants to stop the processing before it finishes, I need to kill cmd.exe. But I have not been able to do that with either of the various methods available. Am I doing something wrong here? Here is a code sample with comments about my findings. #include <Constants.au3> #include <GUIConstantsEx.au3> Local $hGUI = GUICreate("Test", 220, 65) Local $idRun = GUICtrlCreateButton("Run CMD", 20, 20, 80, 25) Local $idStop = GUICtrlCreateButton("Stop", 120, 20, 80, 25) GUISetState(@SW_SHOW, $hGUI) Local $iPID = 0 While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idRun ; Run CMD with Ping command as time delay. $iPID = Run(@ComSpec & ' /c ' & 'ping -n 10 127.0.0.1', @ScriptDir, @SW_SHOW, $STDIN_CHILD + $STDERR_MERGED) ; Cannot be stoped before the -n pings ;~ $iPID = Run(@ComSpec & 'ping -n 20 127.0.0.1', @ScriptDir, @SW_SHOW, $STDOUT_CHILD) ; CMD does not even start ;~ $iPID = Run(@ComSpec & ' /k ' & 'ping -n 10 127.0.0.1', @ScriptDir, @SW_SHOW, $STDOUT_CHILD) ; Does not bring anything Case $idStop ;~ StdinWrite($iPID, "^c") ; Does not seem to stop the process as Ctrl+c would do in the console ;~ ControlSend($iPID,"","","^c") ; Does not seem to stop the process as Ctrl+c would do in the console If $iPID Then ProcessClose($iPID) ; ProcessExists returns 0 after that command but CMD does not stop nor close ;~ If $iPID Then WinClose($iPID) ; No effect ;~ If $iPID Then WinKill($iPID) ; No effect ;~ MsgBox("", "", ProcessExists($iPID), 1) EndSwitch ConsoleWrite(StdoutRead($iPID)) ; Just to see what is happening Sleep(100) WEnd GUIDelete($hGUI) Thanks for your help! Link to comment Share on other sites More sharing options...
CyBoRgWaR Posted December 26, 2016 Share Posted December 26, 2016 want to stop it manually or automatically ? since you can stop manually by CNTRL + C for killing the process use processclose... Example() Func Example() Local $iPID = Run("notepad.exe") WinWait("[CLASS:Notepad]", "", 10) Sleep(2000) . ProcessClose($iPID) EndFunc Link to comment Share on other sites More sharing options...
jguinch Posted December 26, 2016 Share Posted December 26, 2016 I think you must kill the child process : Case $idStop $aPingProcess = _WinAPI_EnumChildProcess ( $iPid ) If Not @error Then ProcessClose($aPingProcess[1][0]) careca and Muxson 2 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Muxson Posted December 26, 2016 Author Share Posted December 26, 2016 @CyBoRgWaR: I want to close it automatically. Did you try my script? Did you understand the Stop button? I did tried ProcessClose as you can see in my script. Thanks for trying... @jguinch: Thanks. You pointed me to the solution which is: I have to kill all child processes. I was not aware of their existence. I added one line to your suggetion and that was it! Case $idStop $aPingProcess = _WinAPI_EnumChildProcess ( $iPid ) If Not @error Then ProcessClose($aPingProcess[1][0]) If Not @error Then ProcessClose($aPingProcess[2][0]) Or better yet: Case $idStop $aPingProcess = _WinAPI_EnumChildProcess ( $iPid ) If Not @error Then For $i=1 to $aPingProcess[0][0] ProcessClose($aPingProcess[$i][0]) Next EndIf Thanks again! Link to comment Share on other sites More sharing options...
jguinch Posted December 26, 2016 Share Posted December 26, 2016 You're welcome Your second piece of code corresponds to what I wanted to guide you. Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
CyBoRgWaR Posted December 27, 2016 Share Posted December 27, 2016 @ jguinch : well im also a newbie in auto it , i just tried to help you out.. better to help peeps... is good to gain more knowledge on autoit... 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