Stephane@202: using System; Stephane@202: using System.Text; Stephane@202: using System.IO; Stephane@202: using System.Windows.Forms; Stephane@202: Stephane@202: namespace SharpDisplayManager Stephane@202: { Stephane@202: public class RichTextBoxTextWriter : TextWriter Stephane@202: { Stephane@202: public delegate void WriteDelegate(char aChar); Stephane@202: Stephane@202: RichTextBox iRichTextBox = null; Stephane@202: string iAccumulator = ""; Stephane@202: Stephane@202: public RichTextBoxTextWriter(RichTextBox aRichTextBox) Stephane@202: { Stephane@202: iRichTextBox = aRichTextBox; Stephane@202: } Stephane@202: Stephane@202: public override void Write(char aChar) Stephane@202: { Stephane@202: base.Write(aChar); Stephane@202: if (iRichTextBox.InvokeRequired) Stephane@202: { Stephane@202: lock (iAccumulator) Stephane@202: { Stephane@202: iAccumulator += aChar; Stephane@202: } Stephane@202: Stephane@202: //WriteDelegate d = new WriteDelegate(Write); Stephane@202: //iRichTextBox.Invoke(d, new object[] { aChar }); Stephane@202: } Stephane@202: else Stephane@202: { Stephane@202: Flush(); Stephane@202: iRichTextBox.AppendText(aChar.ToString()); // When character data is written, append it to the text box. Stephane@202: } Stephane@202: } Stephane@202: Stephane@202: public override Encoding Encoding Stephane@202: { Stephane@202: get { return System.Text.Encoding.UTF8; } Stephane@202: } Stephane@202: Stephane@202: public void FlushAccumulator() Stephane@202: { Stephane@202: lock (iAccumulator) Stephane@202: { Stephane@202: if (!string.IsNullOrEmpty(iAccumulator)) Stephane@202: { Stephane@202: iRichTextBox.AppendText(iAccumulator); // When character data is written, append it to the text box. Stephane@202: iAccumulator = ""; Stephane@202: } Stephane@202: Stephane@202: } Stephane@202: Stephane@202: } Stephane@202: } Stephane@202: }