Home > Backend Development > C++ > Why Does ProcessStartInfo.WaitForExit() Stall When Redirecting StandardOutput or StandardError?

Why Does ProcessStartInfo.WaitForExit() Stall When Redirecting StandardOutput or StandardError?

DDD
Release: 2025-01-29 20:41:09
Original
757 people have browsed it

Why Does ProcessStartInfo.WaitForExit() Stall When Redirecting StandardOutput or StandardError?

solve the problem that the processstartInfo.WaitForexit () is stuck when the regattcing output or standard error of the standard

When the ProcessStartInfo redirects to StandardoutPut or Standarderror, there may be problems. The internal buffer may overflow, leading to a potential deadlocked or blocking your process.

In order to prevent such deadlocks and collect output from two streams, consider using the following asynchronous reading methods:

This asynchronous method ensures that the buffer will not overflow and prevent the system from freezing. Now you can read and collect output without worrying about delayed or potential dead locks. The code has been improved and includes the processing of error output.
<code class="language-csharp">using System.Diagnostics;
using System.Text;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        string filename = "TheProgram.exe";
        string arguments = "some arguments";
        int timeout = 10000; // 10 秒超时

        using (Process process = new Process())
        {
            process.StartInfo.FileName = filename;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;

            StringBuilder output = new StringBuilder();
            StringBuilder error = new StringBuilder();

            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
            using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
            {
                process.OutputDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        outputWaitHandle.Set();
                    }
                    else
                    {
                        output.AppendLine(e.Data);
                    }
                };
                process.ErrorDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        errorWaitHandle.Set();
                    }
                    else
                    {
                        error.AppendLine(e.Data);
                    }
                };

                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                if (process.WaitForExit(timeout) &&
                    outputWaitHandle.WaitOne(timeout) &&
                    errorWaitHandle.WaitOne(timeout))
                {
                    // 进程成功完成。在此处检查 process.ExitCode。
                    Console.WriteLine($"输出: {output}");
                    Console.WriteLine($"错误: {error}"); //添加错误输出
                }
                else
                {
                    // 超时或发生错误。
                    Console.WriteLine("进程超时或失败。");
                }
            }
        }
    }
}</code>
Copy after login

The above is the detailed content of Why Does ProcessStartInfo.WaitForExit() Stall When Redirecting StandardOutput or StandardError?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template