Not able to close cmd.exe when called with /c

6 posts in this topic

Muxson 0

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=0While1SwitchGUIGetMsg()Case$GUI_EVENT_CLOSEExitLoopCase$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 anythingCase$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 consoleIf$iPIDThenProcessClose($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)EndSwitchConsoleWrite(StdoutRead($iPID)); Just to see what is happeningSleep(100)WEndGUIDelete($hGUI)