StephaneLenclud@253: using System; StephaneLenclud@253: using System.Text; StephaneLenclud@253: using System.IO; StephaneLenclud@253: using System.Windows.Forms; StephaneLenclud@253: using System.Diagnostics; StephaneLenclud@253: StephaneLenclud@253: namespace SharpDisplayManager StephaneLenclud@253: { StephaneLenclud@253: public class RichTextBoxTraceListener : TraceListener StephaneLenclud@253: { StephaneLenclud@253: RichTextBox iRichTextBox = null; StephaneLenclud@253: StephaneLenclud@253: public RichTextBoxTraceListener(RichTextBox aRichTextBox) StephaneLenclud@253: { StephaneLenclud@253: iRichTextBox = aRichTextBox; StephaneLenclud@253: } StephaneLenclud@253: StephaneLenclud@253: public override void WriteLine(string aString) StephaneLenclud@253: { StephaneLenclud@253: //Add time stamp and new line characters StephaneLenclud@253: Write(DateTime.Now.ToString("MM/dd HH:mm:ss.fff: ") + aString + "\r\n"); StephaneLenclud@253: } StephaneLenclud@253: StephaneLenclud@253: public override void Write(string aString) StephaneLenclud@253: { StephaneLenclud@253: //Allows iRichTextBox to be updated from different thread StephaneLenclud@253: if (iRichTextBox.InvokeRequired) StephaneLenclud@253: { StephaneLenclud@253: // Fire and forget invocation StephaneLenclud@253: // Using the synchronous variant Invoke tends to result in deadlock here StephaneLenclud@253: iRichTextBox.BeginInvoke(new MethodInvoker(delegate () StephaneLenclud@253: { StephaneLenclud@253: iRichTextBox.Text += aString; StephaneLenclud@253: })); StephaneLenclud@253: } StephaneLenclud@253: else StephaneLenclud@253: { StephaneLenclud@253: iRichTextBox.Text += aString; StephaneLenclud@253: } StephaneLenclud@253: } StephaneLenclud@253: StephaneLenclud@253: } StephaneLenclud@253: }