StandardOutput.ReadLine冻结 [英] StandardOutput.ReadLine Freezes

查看:204
本文介绍了StandardOutput.ReadLine冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我试图读取我所说过程的输出,但是 Readline()每次到达流的末尾时都会冻结(以粗体标记)。我怎么能绕过这个?这是我的代码:



Hi all,

I am trying to read the output from a process that I have stated, but the Readline() freezes every time it reaches the end of the stream (as marked in bold). How can I get around this? Here is my code:

Process nslookup = new Process()
{
    StartInfo = new ProcessStartInfo("nslookup")
    {
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        UseShellExecute = false
    }
};
nslookup.Start();

nslookup.StandardInput.WriteLine("set type=srv");
nslookup.StandardInput.WriteLine("_ldap._tcp.domain.local");
nslookup.StandardInput.Flush();
StringBuilder output = new StringBuilder();
while (nslookup.StandardOutput.Peek() > 0)
{
    output.AppendFormat("{0}\n", nslookup.StandardOutput.ReadLine());
}





非常感谢提前。

亲切的问候,



Many thanks in advance.
Kind regards,

推荐答案

嘿那里,



使用RedirectStandardInput& amp;时,死锁是一个常见的问题。输出。避免这种情况的方法是关闭RedirectIO。在你的例子中你已经冲了它但没有关闭它。



至于输出。逐行阅读只是浪费,因为ReadToEnd()也会提供\\\\ n。如果你只想要\\ n,你可以做一点:



Hey there,

Deadlocks are a common issue when working with RedirectStandardInput & Output. The way to avoid this is to close the RedirectIO. In your example you already flushed it but did not close it.

As for output. Reading line by line is just a waste because ReadToEnd() will also supply the \r\n. And if you want only \n you can do a small:

output = output.Replace("\r\n", "\n");





为了避免脚本死锁,这应该可以解决问题:





To avoid deadlocks in your script this should do the trick:

Process nslookup = new Process()
{
   StartInfo = new ProcessStartInfo("nslookup")
   {
      RedirectStandardInput = true,
      RedirectStandardOutput = true,
      UseShellExecute = false,
      CreateNoWindow = true,
      WindowStyle = ProcessWindowStyle.Hidden
   }
};

nslookup.Start();
nslookup.StandardInput.WriteLine("set type=srv");
nslookup.StandardInput.WriteLine("_ldap._tcp.domain.local"); 
            
nslookup.StandardInput.Flush();
nslookup.StandardInput.Close();

string output = nslookup.StandardOutput.ReadToEnd();

nslookup.WaitForExit();
nslookup.Close();


大家好,



我修改过您的代码可以立即获得流程输出,代码如下:



Hi All,

I have modified your code to get the process output instantly and code is given below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = "test.bat";
            proc.StartInfo.UseShellExecute = false;
           proc.StartInfo.RedirectStandardOutput = true;
            proc.OutputDataReceived += proc_OutputDataReceived;
            proc.Start();
            proc.BeginOutputReadLine();
        }
    }


        void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            this.Dispatcher.Invoke((Action)(() =>
                        {
                            txtprogress.Text = txtprogress.Text + "\n" + e.Data;
                            txtprogress.ScrollToEnd();
                        }));
        }
}







希望这会对你有帮助。



问候,

Ponmalar P




Hope this will help you.

Regards,
Ponmalar P


这篇关于StandardOutput.ReadLine冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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