gpt4 book ai didi

c# - 为什么重新启动进程时不再收到 OutputDataReceived 事件?

转载 作者:行者123 更新时间:2023-12-02 03:38:11 25 4
gpt4 key购买 nike

我有一个 ChildProcessMonitor 类,它启动我的进程,报告收到的数据并在进程退出时重新启动该进程。我的问题是,一旦进程退出并再次调用 Start,就不再报告输出。

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace WcfClient
{
/// <summary>
/// Can be used to launch and monitor (restart on crash) the child process.
/// </summary>
public class ChildProcessMonitor
{
private Process _process;

/// <summary>
/// Starts and monitors the child process.
/// </summary>
/// <param name="fullProcessPath">The full executable process path.</param>
public void StartAndMonitor(string fullProcessPath)
{
StartAndMonitor(fullProcessPath, null);
}

/// <summary>
/// Starts and monitors the child process.
/// </summary>
/// <param name="fullProcessPath">The full executable process path.</param>
/// <param name="arguments">The process arguments.</param>
public void StartAndMonitor(string fullProcessPath, string arguments)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = fullProcessPath,
WorkingDirectory = Path.GetDirectoryName(fullProcessPath) ?? string.Empty,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};

processStartInfo.Arguments = arguments;

_process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
_process.OutputDataReceived += OnOutputDataReceived;
_process.ErrorDataReceived += OnErrorDataReceived;
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
_process.Exited += OnProcessExited;
}

/// <summary>
/// Called when process exits.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void OnProcessExited(object sender, EventArgs e)
{
if (_process != null)
{
Thread.Sleep(2000);
_process.Start();
}
}

/// <summary>
/// The ErrorDataReceived event indicates that the associated process has written to its redirected StandardError stream.
/// </summary>
public DataReceivedEventHandler ErrorDataReceived;

/// <summary>
/// Called when error data received.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Diagnostics.DataReceivedEventArgs"/> instance containing the event data.</param>
private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Trace.WriteLine("Error data.");
if (ErrorDataReceived != null)
{
ErrorDataReceived(sender, e);
}
}

/// <summary>
/// The OutputDataReceived event indicates that the associated Process has written to its redirected StandardOutput stream.
/// </summary>
public DataReceivedEventHandler OutputDataReceived;

/// <summary>
/// Called when output data received.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Diagnostics.DataReceivedEventArgs"/> instance containing the event data.</param>
private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
Trace.WriteLine("Output data.");
if (OutputDataReceived != null)
{
OutputDataReceived(sender, e);
}
}

}

}

最佳答案

尝试使用:

private void OnProcessExited(object sender, EventArgs e)
{
if (_process != null)
{
Thread.Sleep(2000);
_process.CancelOutputRead();
_process.CancelErrorRead();
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();

}
}

ps

简短描述:OutputRead 和 ErrorRead 关闭并重新启动进程。

带有反射代码的详细描述:

public void BeginOutputRead()
{
[..]
if (this.output == null)
{
[..]
this.output = new AsyncStreamReader(this, baseStream, new UserCallBack(this.OutputReadNotifyUser), this.standardOutput.CurrentEncoding);
}
}

public void Start()
{
this.Close();
[..]
}

public void Close()
{
[..]
this.output = null;
this.error = null;
[..]
}

关于c# - 为什么重新启动进程时不再收到 OutputDataReceived 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10919557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com