在 C# 中重用 Process 对象和 IO 重定向

发布于 2024-08-10 12:12:00 字数 1191 浏览 6 评论 0原文

我使用 Process 类的数据就绪事件从正在运行的进程的标准输出和标准错误中获取信息。

它在第一次运行时效果很好,但是在调用 Stop() 然后 Start() 强制重新启动应用程序后,我不再收到数据。我尝试过 CancelErrorRead() 但没有运气。

我正在考虑每次需要重新运行应用程序时重新实例化该对象,但需要这样做似乎很愚蠢。

关于如何重新使用 Process 对象来重新启动已停止的进程有什么建议吗?

相关代码块:

构造函数:

   ProcessStartInfo objStartInfo = new ProcessStartInfo();
    objStartInfo.CreateNoWindow = true;
    objStartInfo.RedirectStandardInput = true;
    objStartInfo.RedirectStandardOutput = true;
    objStartInfo.RedirectStandardError = true;
    objStartInfo.UseShellExecute = false;

    objClient = new Process();
    objClient.StartInfo = objStartInfo;

    objClient.EnableRaisingEvents = true;
    objClient.OutputDataReceived   += new DataReceivedEventHandler(read);
    objClient.ErrorDataReceived    += new DataReceivedEventHandler(error);

开始:

    objClient.StartInfo.FileName    = strAppPath;
    objClient.StartInfo.Arguments   = strArgs;
    start();
    objClient.BeginErrorReadLine();
    objClient.BeginOutputReadLine();

停止:

    objClient.Close();
    objClient.CancelErrorRead();
    objClient.CancelOutputRead();

I'm using the data ready events of the Process class to get information from the standard output and standard error of a running process.

It works great on the first run, but after calling Stop() then Start() to force a restart of the application, I no longer recieve data. I've tried CancelErrorRead() but no luck there.

I'm considering just re-instantiating the object every time I need to re-run the app, but it seems silly to need to do that.

Any advice on how to re-use a Process object to restart a stopped process?

Relevant code chunks:

Constructor:

   ProcessStartInfo objStartInfo = new ProcessStartInfo();
    objStartInfo.CreateNoWindow = true;
    objStartInfo.RedirectStandardInput = true;
    objStartInfo.RedirectStandardOutput = true;
    objStartInfo.RedirectStandardError = true;
    objStartInfo.UseShellExecute = false;

    objClient = new Process();
    objClient.StartInfo = objStartInfo;

    objClient.EnableRaisingEvents = true;
    objClient.OutputDataReceived   += new DataReceivedEventHandler(read);
    objClient.ErrorDataReceived    += new DataReceivedEventHandler(error);

Start:

    objClient.StartInfo.FileName    = strAppPath;
    objClient.StartInfo.Arguments   = strArgs;
    start();
    objClient.BeginErrorReadLine();
    objClient.BeginOutputReadLine();

Stop:

    objClient.Close();
    objClient.CancelErrorRead();
    objClient.CancelOutputRead();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-08-17 12:12:00

在调用 Start()(或使用 Process 中的静态方法之一)之前,您的 Process 对象不会与进程关联。停止/关闭的进程在功能上与根本没有进程相同。鉴于此,与在 Windows 上创建进程的(相对巨大的)成本相比,很难相信创建新的 Process 对象会产生任何开销。只需根据需要创建新的 Process 对象即可。

Your Process object is not associated with a process until you call Start() (or use one of the static methods off Process). A stopped/closed process is functionally the same as no process at all. Given that, it's hard to believe there's any overhead to creating a new Process object, when compared to the (relatively enormous) cost of creating processes on Windows. Just create new Process objects as needed.

来世叙缘 2024-08-17 12:12:00

根据 msdn,您应该调用 BeginOutputReadLineBeginErrorReadLine 来启用使用事件从 StandardOutput 或 StandardError 进行异步读取。

看一下评论部分
BeginOutputReadLine

According to msdn you should call BeginOutputReadLine and BeginErrorReadLine to enable asynchronous reads from StandardOutput or StandardError using events.

Have a look at the remarks section on
BeginOutputReadLine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文