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 = ""; StephaneLenclud@206: private char iLastChar='\n'; Stephane@202: Stephane@202: public RichTextBoxTextWriter(RichTextBox aRichTextBox) Stephane@202: { StephaneLenclud@206: iRichTextBox = aRichTextBox; Stephane@202: } Stephane@202: Stephane@202: public override void Write(char aChar) Stephane@202: { StephaneLenclud@206: if (aChar == '\r') StephaneLenclud@206: { StephaneLenclud@206: //Skip StephaneLenclud@206: return; StephaneLenclud@206: } StephaneLenclud@206: StephaneLenclud@206: //Put our time stamp if starting a new line StephaneLenclud@206: char previousChar = iLastChar; StephaneLenclud@206: iLastChar = aChar; StephaneLenclud@206: if (previousChar == '\n') StephaneLenclud@206: { StephaneLenclud@208: //Write(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss.fff: ")); StephaneLenclud@208: Write(DateTime.Now.ToString("MM/dd HH:mm:ss.fff: ")); StephaneLenclud@206: } StephaneLenclud@206: StephaneLenclud@206: 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: StephaneLenclud@206: //Invoke was not working from here 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. StephaneLenclud@206: } Stephane@202: } Stephane@202: Stephane@202: public override Encoding Encoding Stephane@202: { Stephane@202: get { return System.Text.Encoding.UTF8; } Stephane@202: } Stephane@202: StephaneLenclud@206: /// StephaneLenclud@206: /// StephaneLenclud@206: /// StephaneLenclud@206: public override void Flush() Stephane@202: { StephaneLenclud@206: base.Flush(); StephaneLenclud@206: 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: }