C#:读取子流程stdout块,直到ANOTHER子流程完成? [英] C#: Read on subprocess stdout blocks until ANOTHER subprocess finishes?

查看:145
本文介绍了C#:读取子流程stdout块,直到ANOTHER子流程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来启动子进程并监视其输出的C#代码:

Here is the C# code I'm using to launch a subprocess and monitor its output:

using (process = new Process()) {
    process.StartInfo.FileName = executable;
    process.StartInfo.Arguments = args;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.CreateNoWindow = true;
    process.Start();

    using (StreamReader sr = process.StandardOutput) {
        string line = null;
        while ((line = sr.ReadLine()) != null) {
            processOutput(line);
        }
    }

    if (process.ExitCode == 0) {
        jobStatus.State = ActionState.CompletedNormally;
        jobStatus.Progress = 100;
    } else {
        jobStatus.State = ActionState.CompletedAbnormally;
    }
    OnStatusUpdated(jobStatus);
}

我要在单独的ThreadPool线程中启动多个子进程(但在四核计算机上一次不能超过四个).一切正常.

I am launching multiple subprocesses in separate ThreadPool threads (but no more than four at a time, on a quad-core machine). This all works fine.

我遇到的问题是我的一个子进程将退出,但是对sr.ReadLine()的相应调用将被阻塞,直到我的一个子进程中的另一个退出.我不确定返回的内容,但是除非我缺少某些东西,否则这不会发生.

The problem I am having is that one of my subprocesses will exit, but the corresponding call to sr.ReadLine() will block until ANOTHER one of my subprocesses exits. I'm not sure what it returns, but this should NOT be happening unless there is something I am missing.

关于我的子流程,没有什么可以导致它们以任何方式链接"的-它们彼此之间无法通信.发生这种情况时,我什至可以在Task Manager/Process Explorer中查看,看看我的子进程实际上已经退出了,但是在其标准输出上对ReadLine()的调用仍然处于阻塞状态!

There's nothing about my subprocess that would cause them to be "linked" in any way - they don't communicate with each other. I can even look in Task Manager / Process Explorer when this is happening, and see that my subprocess has actually exited, but the call to ReadLine() on its standard output is still blocking!

我可以通过将输出监视代码旋转到新线程中并执行process.WaitForExit()来解决此问题,但这似乎是很奇怪的行为.有人知道这是怎么回事吗?

I've been able to work around it by spinning the output monitoring code out into a new thread and doing a process.WaitForExit(), but this seems like very odd behavior. Anyone know what's going on here?

推荐答案

我认为问题不在于您的代码.阻止调用可以取消阻止的原因有很多,不仅是因为他们的任务已经完成.

I think it's not your code that's the issue. Blocking calls can unblock for a number of reasons, not only because their task was accomplished.

我必须承认我不了解Windows,但是在Unix世界中,当一个孩子完成时,一个信号会发送到父进程,这会使他摆脱任何阻塞调用.这样可以取消对父母期望的任何输入的读取.

I don't know about Windows, I must admit, but in the Unix world, when a child finishes, a signal is sent to the parent process and this wakes him from any blocking calls. This would unblock a read on whatever input the parent was expecting.

如果Windows能够类似地工作,这也不会令我感到惊讶.无论如何,请阅读阻止呼叫可能解除阻止的原因.

It wouldn't surprise me if Windows worked similarly. In any case, read up on the reasons why a blocking call may unblock.

这篇关于C#:读取子流程stdout块,直到ANOTHER子流程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆