Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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,
Posted

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:

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


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

C#
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();
 
Share this answer
 
Hi All,

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

C#
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();
                        }));
        }
}



Hope this will help you.

Regards,
Ponmalar P
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900