Thursday, 5 September 2013

Redirecting console output to listbox from another thread throws InvalidOperationException

Redirecting console output to listbox from another thread throws
InvalidOperationException

I'm trying to redirect console output to a listbox, but when I write text
to the console from a different thread it throws an
InvalidOperationException
ListBoxWriter:
public class ListBoxWriter : TextWriter
{
private ListBox list;
private StringBuilder content = new StringBuilder();
public ListBoxWriter(ListBox list)
{
this.list = list;
}
public override void Write(char value)
{
base.Write(value);
content.Append(value);
if (value == '\n')
{
list.Items.Add(content.ToString());
content = new StringBuilder();
}
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}

No comments:

Post a Comment