Server/RichTextBoxTextWriter.cs
author StephaneLenclud
Tue, 26 Jul 2016 15:30:46 +0200
changeset 221 5770478e1fe3
parent 206 33be8cb90c57
permissions -rw-r--r--
Improved support for action enum properties.
Stephane@202
     1
using System;
Stephane@202
     2
using System.Text;
Stephane@202
     3
using System.IO;
Stephane@202
     4
using System.Windows.Forms;
Stephane@202
     5
Stephane@202
     6
namespace SharpDisplayManager
Stephane@202
     7
{
Stephane@202
     8
    public class RichTextBoxTextWriter : TextWriter
Stephane@202
     9
    {
Stephane@202
    10
        public delegate void WriteDelegate(char aChar);
Stephane@202
    11
Stephane@202
    12
        RichTextBox iRichTextBox = null;
Stephane@202
    13
        string iAccumulator = "";
StephaneLenclud@206
    14
        private char iLastChar='\n';
Stephane@202
    15
Stephane@202
    16
        public RichTextBoxTextWriter(RichTextBox aRichTextBox)
Stephane@202
    17
        {
StephaneLenclud@206
    18
            iRichTextBox = aRichTextBox;            
Stephane@202
    19
        }
Stephane@202
    20
Stephane@202
    21
        public override void Write(char aChar)
Stephane@202
    22
        {
StephaneLenclud@206
    23
            if (aChar == '\r')
StephaneLenclud@206
    24
            {
StephaneLenclud@206
    25
                //Skip
StephaneLenclud@206
    26
                return;
StephaneLenclud@206
    27
            }
StephaneLenclud@206
    28
StephaneLenclud@206
    29
            //Put our time stamp if starting a new line
StephaneLenclud@206
    30
            char previousChar = iLastChar;
StephaneLenclud@206
    31
            iLastChar = aChar;
StephaneLenclud@206
    32
            if (previousChar == '\n')
StephaneLenclud@206
    33
            {
StephaneLenclud@208
    34
                //Write(DateTime.Now.ToString("yyyy/MM/dd - HH:mm:ss.fff: "));
StephaneLenclud@208
    35
                Write(DateTime.Now.ToString("MM/dd HH:mm:ss.fff: "));
StephaneLenclud@206
    36
            }
StephaneLenclud@206
    37
StephaneLenclud@206
    38
            
Stephane@202
    39
            base.Write(aChar);
Stephane@202
    40
            if (iRichTextBox.InvokeRequired)
Stephane@202
    41
            {
Stephane@202
    42
                lock (iAccumulator)
Stephane@202
    43
                {
Stephane@202
    44
                    iAccumulator += aChar;
Stephane@202
    45
                }
Stephane@202
    46
                
StephaneLenclud@206
    47
                //Invoke was not working from here
Stephane@202
    48
                //WriteDelegate d = new WriteDelegate(Write);
Stephane@202
    49
                //iRichTextBox.Invoke(d, new object[] { aChar });
Stephane@202
    50
            }
Stephane@202
    51
            else
Stephane@202
    52
            {
Stephane@202
    53
                Flush();
Stephane@202
    54
                iRichTextBox.AppendText(aChar.ToString()); // When character data is written, append it to the text box.
StephaneLenclud@206
    55
            }
Stephane@202
    56
        }
Stephane@202
    57
Stephane@202
    58
        public override Encoding Encoding
Stephane@202
    59
        {
Stephane@202
    60
            get { return System.Text.Encoding.UTF8; }
Stephane@202
    61
        }
Stephane@202
    62
StephaneLenclud@206
    63
        /// <summary>
StephaneLenclud@206
    64
        /// 
StephaneLenclud@206
    65
        /// </summary>
StephaneLenclud@206
    66
        public override void Flush()
Stephane@202
    67
        {
StephaneLenclud@206
    68
            base.Flush();
StephaneLenclud@206
    69
Stephane@202
    70
            lock (iAccumulator)
Stephane@202
    71
            {
Stephane@202
    72
                if (!string.IsNullOrEmpty(iAccumulator))
Stephane@202
    73
                {
Stephane@202
    74
                    iRichTextBox.AppendText(iAccumulator); // When character data is written, append it to the text box.
Stephane@202
    75
                    iAccumulator = "";
Stephane@202
    76
                }
Stephane@202
    77
            }
Stephane@202
    78
Stephane@202
    79
        }
Stephane@202
    80
    }
Stephane@202
    81
}