Question

I am having difficulty redirecting the output from a console application to a Windows Form App, when the process is created in a separate *.dll file (excuse the sloppy terminology, but I'm new to programming). I came across this link (and I may pursue his method), detailing a similar problem: http://www.codeproject.com/KB/threads/launchprocess.aspx?msg=3087118 I can read one line from the console, but how to get it to stay open?

So basically my question is how one accesses streamreader output from within a separate class properly?

The code below works perfectly when called locally.

private void exampleErrorRedirection()
    {
        Process proc = new Process();
        proc.StartInfo.FileName = /exampleconsoleapp.exe;
        proc.StartInfo.Arguments = "some arguments that work";
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = true;
        if (!proc.Start())
        {
            Console.WriteLine("Error starting");
            return;
        }
        StreamReader reader = proc.StandardError;
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            textBoxForStandardError.Text = line;
        }
        proc.Close();
    }

However I want to be able to have the output redirected from a separate class when called. Atm I can only get the first line from the console, and it does not update.

private void exampleErrorRedirection()
    {

        exampleDLLFile.startProc ConsoleApp new exampleDLLFile.startProc();

        ConsoleApp.Run();

        while (convert.line != null)
        {
            textBoxForStandardError.Text = ConsoleApp.line;
        }
}

where the class houses a method like so:

public class convertFile
{

    public string line;

    public void Run()
    {
        Process proc = new Process();
        proc.StartInfo.FileName = /exampleconsoleapp.exe;
        proc.StartInfo.Arguments = "some arguments that work";
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = true;

        if (!proc.Start())
        {
            Console.WriteLine("Error starting");

        }

        StreamReader reader = proc.StandardError;
        while ((line = reader.ReadLine()) != null)
        {
            line = reader.ReadLine();
        }

        proc.Close();

    }
}
Was it helpful?

Solution

You need to add a DataReceivedEventHandler for the event Process.ErrorDataReceived:

// process is Process
process.UseShellExecute = false;
process.RedirectStandardError = true;
process.ErrorDataReceived += DataReceived;
process.Start();
process.BeginErrorReadLine(); // start asynchronous error read
.
.
.
process.CancelErrorRead();

void DataReceived(object sender, DataReceivedEventArgs e) {
    // e.Data is line of redirected standard error
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top