Reputation: 1572
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 ?
Upvotes: 3
Views: 675
Reputation: 1572
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).
Upvotes: 1