cmd进程没有退出

时间:2013-01-19 11:23:18

标签: vb.net cmd

我正在运行以下代码来执行来自vb.net app的命令。

    Dim CMDServer As Diagnostics.ProcessStartInfo
    Dim CMDReply As Diagnostics.Process
    CMDServer = New Diagnostics.ProcessStartInfo
    CMDServer.WorkingDirectory = "C:/"
    CMDServer.FileName = "cmd.exe"
    CMDServer.UseShellExecute = False
    CMDServer.RedirectStandardOutput = True
    CMDServer.RedirectStandardError = True
    CMDServer.CreateNoWindow = True
    CMDServer.Arguments = "/C " + command
    CMDReply = Process.Start(CMDServer)
    MsgBox("START")
    Dim Reply As String = ""
    If Not CMDReply.HasExited Then
        CMDReply.WaitForExit()
    End If
    MsgBox("EXIT")
    Try
        Dim ext = CMDReply.ExitCode
        Reply = CMDReply.StandardOutput.ReadToEnd()
        MsgBox(ext.ToString + "   " + Reply)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

它适用于几乎所有命令,但当command="help"程序卡在CMDReply.WaitForExit()上时。任何人都可以解释这里可能出现的问题吗?

1 个答案:

答案 0 :(得分:1)

  

标准死锁。首先读取输出然后等待退出。该   程序无法退出,直到您已读取其所有输出。代码你   只有在适合缓冲区的输出很少时才能工作。

来自Hans Passant的评论