我怎么能检查过程中得到的输出? [英] how can i check if the process got output?

查看:116
本文介绍了我怎么能检查过程中得到的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来表示,如果像标准计算器的过程有一个输出与否,结果
I需要它,因为我有这样一行:

  SR = p1.StandardOutput; 

和我需要做到这一点:

  S = sr.ReadLine(); 



只有当有一个从 P1 $一个输出在计算器示例b $ b没有输出,让的ReadLine 。结果
谢谢大家。


$卡住后的程序b $ b

中的代码:

 而(I< asProcesses.Length  -  1)
$ { b $ b如果(第(i + 1)== asProcesses.Length - 1安培;&安培; sOutRedirect =!)
中断;
p1.StartInfo.RedirectStandardOutput = TRUE;
p1.StartInfo.FileName = asProcesses [I]
p1.StartInfo.UseShellExecute = FALSE;
如果(我== 0)
p1.Start();
SR = p1.StandardOutput;
处理P2 =新工艺();
p2.StartInfo.RedirectStandardInput = TRUE;
p2.StartInfo.FileName = asProcesses [I + 1];
p2.StartInfo.UseShellExecute = FALSE;
p2.Start();
SW = p2.StandardInput;
,而(sr.EndOfStream&安培;!&安培;!S = NULL)
{
S = sr.ReadLine();
如果(S!= NULL)
{
sw.WriteLine(S);
}
}
如果(SW!= NULL)
sw.Close();
如果(SR!= NULL)
sr.Close();
I ++;
}


解决方案

如果执行后所有的进程退出,用这个来代替内,同时:

  p1.WaitForExit(); 
sw.Write(sr.ReadToEnd());

如果你想进程超时:

  INT I = 0; 
,而(p1.HasExited&安培;!&安培; I< maxWaits)
{
Thread.sleep代码(延迟);
I ++;
}
sw.Write(sr.ReadToEnd());

//杀过程中,如果运行:
如果(!p1.HasExited)
{
尝试{p1.Kill(); }
赶上{}
}



编辑:



看来你正试图从链每个流程到下一个输出。如果是这样的话,你错过了一个 P1 = P2 在循环的末尾。结果
此外,考虑了移动的第一个启动过程循环的:它会让你的代码更具有可读性。设置的StartInfo的应该被移动到 P1 如果(我== 0)块,如果你离开它这样。移动输出读最后加工出来不会是一个糟糕的主意,在我看来......



编辑:



这是我的解决方案(带超时):

  INT maxWaits = 10; //最多等待1秒。 
INT延迟= 100;

变种P =新的Process();
p.StartInfo.RedirectStandardOutput = TRUE;
p.StartInfo.FileName = asProcesses [0];
p.StartInfo.UseShellExecute = FALSE;
p.Start();

的foreach(在asProcesses.Skip变种路径(1))
{
变种P2 =新工艺();
p2.StartInfo.FileName =路径;
p2.StartInfo.RedirectStandardInput = TRUE;
p2.StartInfo.RedirectStandardOutput = TRUE;
p2.StartInfo.UseShellExecute = FALSE;

{
INT I = 0;
,而(p.HasExited&安培;!&安培; I< maxWaits)
{
p2.StandardInput.Write(p.StandardOutput.ReadToEnd()); //重定向IO。这条线表示第二过程可以开始计算如果第一是长期运行,并逐步将其输出。
Thread.sleep代码(延迟);
I ++;
}
}

p2.StandardInput.Write(p.StandardOutput.ReadToEnd()); //最后输出重定向从页。

{
//杀过程中,如果仍然在运行:
如果(p.HasExited!)
{
尝试{p.Kill(); }
赶上{}
}
}
}

{
INT I = 0;
,而(p.HasExited&安培;!&安培; I< maxWaits)
{
Thread.sleep代码(延迟);
I ++;
}
}

字符串结果= p.StandardOutput.ReadToEnd();
{
如果
{
尝试{p.Kill()(p.HasExited!); }
赶上{}
}
}



编辑:



算法,等待每一个进程退出:

  VAR p =新工艺(); 
p.StartInfo.RedirectStandardOutput = TRUE;
p.StartInfo.FileName = asProcesses [0];
p.StartInfo.UseShellExecute = FALSE;
p.Start();

的foreach(在asProcesses.Skip变种路径(1))
{
变种P2 =新工艺();
p2.StartInfo.FileName =路径;
p2.StartInfo.RedirectStandardInput = TRUE;
p2.StartInfo.RedirectStandardOutput = TRUE;
p2.StartInfo.UseShellExecute = FALSE;

p.WaitForExit();
p2.StandardInput.Write(p.StandardOutput.ReadToEnd());
}

p.WaitForExit();
字符串结果= p.StandardOutput.ReadToEnd();



我感动的第一道工序圈外摆脱有条件的。控制流是简单的这种方式,它更容易专门添加代码亲情的第一道工序。


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++;
            }

解决方案

If all processes exit after executing, use this instead of the inner while:

p1.WaitForExit();
sw.Write(sr.ReadToEnd());

If you want the processes to time out:

int i = 0;
while (!p1.HasExited && i < maxWaits)
{
    Thread.Sleep(delay);
    i++;
}
sw.Write(sr.ReadToEnd());

//Kill process if running:
if (!p1.HasExited)
{
    try { p1.Kill(); }
    catch { }
}

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 the if (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):

        int maxWaits = 10; // Wait 1 second at most.
        int delay = 100;

        var p = new Process();
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = asProcesses[0];
        p.StartInfo.UseShellExecute = false;
        p.Start();

        foreach (var path in asProcesses.Skip(1))
        {
            var p2 = new Process();
            p2.StartInfo.FileName = path;
            p2.StartInfo.RedirectStandardInput = true;
            p2.StartInfo.RedirectStandardOutput = true;
            p2.StartInfo.UseShellExecute = false;

            {
                int i = 0;
                while (!p.HasExited && i < maxWaits)
                {
                    p2.StandardInput.Write(p.StandardOutput.ReadToEnd()); //Redirect IO. This line means that the second process can start calculations if the first is long-running and writes its output progressively.
                    Thread.Sleep(delay);
                    i++;
                }
            }

            p2.StandardInput.Write(p.StandardOutput.ReadToEnd()); //Redirect last output from p.

            {
                //Kill process if still running:
                if (!p.HasExited)
                {
                    try { p.Kill(); }
                    catch { }
                }
            }
        }

        {
            int i = 0;
            while (!p.HasExited && i < maxWaits)
            {
                Thread.Sleep(delay);
                i++;
            }
        }

        string result = p.StandardOutput.ReadToEnd();
        {
            if (!p.HasExited)
            {
                try { p.Kill(); }
                catch { }
            }
        }

Edit:

Algorithm that waits for each process to exit:

        var p = new Process();
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = asProcesses[0];
        p.StartInfo.UseShellExecute = false;
        p.Start();

        foreach (var path in asProcesses.Skip(1))
        {
            var p2 = new Process();
            p2.StartInfo.FileName = path;
            p2.StartInfo.RedirectStandardInput = true;
            p2.StartInfo.RedirectStandardOutput = true;
            p2.StartInfo.UseShellExecute = false;

            p.WaitForExit();
            p2.StandardInput.Write(p.StandardOutput.ReadToEnd());
        }

        p.WaitForExit();
        string result = p.StandardOutput.ReadToEnd();

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.

这篇关于我怎么能检查过程中得到的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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