3

I am redirecting my console to a TextBox using the following setup :

public partial class WindowMain : Window
{
    public WindowMain()
    {
        InitializeComponent(); 
        TextWriter writer = new TextBoxStreamWriter(consoleTextBox);
        Console.SetOut(writer);
    }
}

with class TextBoxStreamWriter :

public class TextBoxStreamWriter : TextWriter
{
    TextBox _output = null;

    public TextBoxStreamWriter(TextBox output)
    {
        _output = output;
    }

    public override void Write(char value)
    {
        base.Write(value);
        _output.Dispatcher.Invoke(new Action(() =>
        {
            _output.AppendText(value.ToString());
        })
        );
    }
}

This is working. When I call Console.Write in my application my TextBox is appended correctly. The problem is I am feeding the console with very large data continuously (output of a very verbose batch launched via a Process calling cmd.exe)

process.OutputDataReceived += (s, e) =>
{
    if (e.Data != null)
    {
        Console.WriteLine(e.Data);
    }
};

The issue here is that this is slowing down the whole application (particularly threads that I launch during the feeding). My idea is to manually control the frequency of writing. However I feel this is a common issue and I don't want to reinvent the wheel. Any suggestions ?

1
  • I am sure this is possible with the Reactive Extensions. Unfortunatelly, I don't have experience with them, so I can't provide an actual example. But you could start looking in that direction if you don't get any answer here. Feb 5, 2013 at 12:32

1 Answer 1

1

I ended up not using the console anymore. My application has no need for a console indeed.

process.OutputDataReceived += (s, e) =>
{
    if (e.Data != null)
    {
        consoleTextBox.AppendText(e.Data);
    }
};

My solution was more complex than the piece of code above since I use threads (which is what confused my in the first place, making me think using the console was smart).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.