Server/RichTextBoxTraceListener.cs
author StephaneLenclud
Tue, 30 Aug 2016 03:45:45 +0200
changeset 258 e237c2e33545
permissions -rw-r--r--
Published v1.1.0.0.
EAR is now async.
     1 using System;
     2 using System.Text;
     3 using System.IO;
     4 using System.Windows.Forms;
     5 using System.Diagnostics;
     6 
     7 namespace SharpDisplayManager
     8 {
     9     public class RichTextBoxTraceListener : TraceListener
    10     {
    11         RichTextBox iRichTextBox = null;
    12 
    13         public RichTextBoxTraceListener(RichTextBox aRichTextBox)
    14         {
    15             iRichTextBox = aRichTextBox;
    16         }
    17 
    18         public override void WriteLine(string aString)
    19         {
    20             //Add time stamp and new line characters
    21             Write(DateTime.Now.ToString("MM/dd HH:mm:ss.fff: ") + aString + "\r\n");
    22         }
    23 
    24         public override void Write(string aString)
    25         {
    26             //Allows iRichTextBox to be updated from different thread
    27             if (iRichTextBox.InvokeRequired)
    28             {
    29                 // Fire and forget invocation
    30                 // Using the synchronous variant Invoke tends to result in deadlock here
    31                 iRichTextBox.BeginInvoke(new MethodInvoker(delegate ()
    32                 {
    33                     iRichTextBox.Text += aString;
    34                 }));
    35             }
    36             else
    37             {
    38                 iRichTextBox.Text += aString;
    39             }
    40         }
    41 
    42     }
    43 }