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