Example #1
0
def StartCmdProcess(commandLine):
    # NOTE: do not call Process.WaitForExit() until redirected streams have been entirely read from / closed.
    #       doing so can lead to a deadlock when the child process is waiting on being able to write to output / error
    #       stream and the parent process is waiting for the child to exit! See Microsoft's documentation for more info.
    # NOTE: if redirecting both output and error streams, one should be read asynchronously to avoid a deadlock where
    #       the child process is waiting to write to one of the streams and the parent is waiting for data from the other
    #       stream. See Microsoft's documentation for more info.
    psi = ProcessStartInfo('cmd.exe', '/U /S /C " ' + commandLine + ' "')
    psi.UseShellExecute = False
    psi.CreateNoWindow = True
    psi.RedirectStandardInput = False
    psi.RedirectStandardError = False  # See notes above if enabling this alongside redirect output stream.
    psi.RedirectStandardOutput = True
    psi.StandardOutputEncoding = Encoding.Unicode
    p = Process.Start(psi)
    return p