应用程序冻结在“StandardOutput.ReadLine()”上

本文关键字:ReadLine StandardOutput 冻结 应用程序 | 更新日期: 2023-09-27 18:32:08

应用程序在尝试调用"StandardOutput.ReadLine()"时挂起。

代码:

ProcessStartInfo startInfo = new ProcessStartInfo("c:''windows''system32''myTesting.exe");
            String s = " ";
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            Process p = Process.Start(startInfo);
            p.StandardInput.WriteLine("list volume'n");
            String f = "";
            while (!p.StandardOutput.EndOfStream)
                {
                    s = p.StandardOutput.ReadLine();
                }

有时会发生"死锁异常"错误,但并非总是如此。

应用程序冻结在“StandardOutput.ReadLine()”上

也许问题

出在了。试试这个:

// Create the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 //setting the application path
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();