views:

54

answers:

1

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();

    }
}
+1  A: 

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
}
Jason
Are you able to explain the code more - thanks in advance.
Dominic Bou-Samra
I added a little more code showing how to use it properly. Can you ask a more specific question?
Jason
I'm still not understanding how one accesses e.Data from outside the class. Seriously nooby but I'm struggling :(
Dominic Bou-Samra
Outside which class?
Jason
Well the process is starting from within a separate class. How do I redirect the e.Data, so I can update my textbox? I can't figure out how to get the textbox to equal the e.data somehow.
Dominic Bou-Samra
Ignore that. Turns out I was subscribing to the event AFTER the process had finished. All good now
Dominic Bou-Samra
Great! I'm glad this was helpful for you. :-)
Jason