我如何检查进程是否有输出?
有没有办法指示标准计算器等进程是否有输出,
我需要它,因为我有这一行:
sr = p1.StandardOutput;
并且我需要这样做:
s = sr.ReadLine();
仅当有 p1
的输出时 例如,在计算器中,没有输出,因此程序在 ReadLine
之后卡住。
谢谢大家。
代码:
while (i < asProcesses.Length - 1)
{
if ((i + 1) == asProcesses.Length - 1 && sOutRedirect != "")
break;
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.FileName = asProcesses[i];
p1.StartInfo.UseShellExecute = false;
if(i==0)
p1.Start();
sr = p1.StandardOutput;
Process p2 = new Process();
p2.StartInfo.RedirectStandardInput = true;
p2.StartInfo.FileName = asProcesses[i + 1];
p2.StartInfo.UseShellExecute = false;
p2.Start();
sw = p2.StandardInput;
while (!sr.EndOfStream && s != null)
{
s = sr.ReadLine();
if (s != null)
{
sw.WriteLine(s);
}
}
if (sw != null)
sw.Close();
if (sr != null)
sr.Close();
i++;
}
is there a way to indicate if a process like standard calculator got an output or not,
i need it because i have this line :
sr = p1.StandardOutput;
and i need to do this :
s = sr.ReadLine();
only if there's an output from p1
in calculator for example there's no output so the program stuck after the ReadLine
.
thanks all.
the code :
while (i < asProcesses.Length - 1)
{
if ((i + 1) == asProcesses.Length - 1 && sOutRedirect != "")
break;
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.FileName = asProcesses[i];
p1.StartInfo.UseShellExecute = false;
if(i==0)
p1.Start();
sr = p1.StandardOutput;
Process p2 = new Process();
p2.StartInfo.RedirectStandardInput = true;
p2.StartInfo.FileName = asProcesses[i + 1];
p2.StartInfo.UseShellExecute = false;
p2.Start();
sw = p2.StandardInput;
while (!sr.EndOfStream && s != null)
{
s = sr.ReadLine();
if (s != null)
{
sw.WriteLine(s);
}
}
if (sw != null)
sw.Close();
if (sr != null)
sr.Close();
i++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果所有进程在执行后退出,请使用它而不是内部 while:
如果您希望进程超时:
编辑:
看来您正在尝试将每个进程的输出链接到下一个进程。如果是这种情况,则说明您在循环末尾缺少
p1 = p2
。另外,考虑将第一个启动过程移出循环:这将使您的代码更具可读性。如果您这样保留,则应将
p1
的 StartInfo 的设置移至if (i == 0)
块中。在我看来,将输出读取的最后一个进程移出也不是一个坏主意...编辑:
这是我的解决方案(超时):
编辑:
等待每个进程退出的算法:
我移动了第一个进程跳出循环以摆脱条件。这种方式控制流程更简单,并且更容易专门向第一个进程添加代码影响。
If all processes exit after executing, use this instead of the inner while:
If you want the processes to time out:
Edit:
It seems you're trying to chain the output from each process to the next one. If that's the case, you're missing a
p1 = p2
at the end of the loop.Also, consider to move the first starting process out of the loop: It will make your code much more readable. Setting
p1
's StartInfo should be moved into theif (i == 0)
block if you leave it this way. Moving the output read last process out wouldn't be a bad idea either, in my opinion...Edit:
This is my solution (with timing out):
Edit:
Algorithm that waits for each process to exit:
I moved the first process out of the loop to get rid of the conditional. The control flow is simpler this way and it's easier to add code affection the first process specifically.