StandardOutput.ReadToEnd() hangs in one machine but never in another

91 Views Asked by At

Below codes have been always working in one environment.

Now we switched to a new environment (new VM), this codes do not work any more, it just hangs there until I manually close the pop-up window from cmd.exe. Obviously, there is some dead lock. But why it only happens in the new environment?? Even now it still works in the old environment.

        public static void Main()
    {
        Process p = new Process()
        {
            StartInfo = new ProcessStartInfo()
            {
                FileName = "c:\\windows\\System32\\cmd.exe",
                Arguments = "/C START \"exeName\" /D %SystemDrive%\\somefolder start.bat",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                WorkingDirectory = "c:\\windows\\System32",
                CreateNoWindow = true
            }
        };
        p.Start();

        string cv_error = null;
        Thread et = new Thread(() => { cv_error = p.StandardError.ReadToEnd(); });
        et.Start();

        string cv_out = null;
        Thread ot = new Thread(() => { cv_out = p.StandardOutput.ReadToEnd(); });
        ot.Start();

        p.WaitForExit();
        Console.WriteLine("process finish " + p.HasExited); // This line prints "process finish true" successfully
        ot.Join(); // Stuck here until I manually close the popup window. But it works in another machine with this popup window stay open
        et.Join();

        Console.WriteLine("success" + p.ExitCode);
        Console.ReadLine();
    }

The start.bat looks like

@echo off
Echo "something"
mklink /D Logs "D:/logs"

Echo "Starting"
cd %~dp0\someFolder
.\ExeToInstallTools.exe arg1 // it will popup a console window showing the progress and logs

IF ERRORLEVEL 1 goto error_handler
Exit /B 0

:error_handler
Echo "Failed"
Exit /B 1

UPDATE:

I have try to use some stackTrace tool to find the difference: when my app (process=4736) start the cmd process, cmd.exe /C turns into cmd.exe /K, it does not happen in the old machine.

StackTraceToolView

0

There are 0 best solutions below