Jump to content

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


Muxson
 Share

Recommended Posts

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

I think you must kill the child process :

Case $idStop
  $aPingProcess = _WinAPI_EnumChildProcess ( $iPid )
  If Not @error Then ProcessClose($aPingProcess[1][0])

 

Link to comment
Share on other sites

@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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...