Server/RichTextBoxTextWriter.cs
author Stephane Lenclud
Mon, 18 Jul 2016 15:56:25 +0200
changeset 205 127382a596e7
child 206 33be8cb90c57
permissions -rw-r--r--
Published v0.9.2.0
     1 using System;
     2 using System.Text;
     3 using System.IO;
     4 using System.Windows.Forms;
     5 
     6 namespace SharpDisplayManager
     7 {
     8     public class RichTextBoxTextWriter : TextWriter
     9     {
    10         public delegate void WriteDelegate(char aChar);
    11 
    12         RichTextBox iRichTextBox = null;
    13         string iAccumulator = "";
    14 
    15         public RichTextBoxTextWriter(RichTextBox aRichTextBox)
    16         {
    17             iRichTextBox = aRichTextBox;
    18         }
    19 
    20         public override void Write(char aChar)
    21         {
    22             base.Write(aChar);
    23             if (iRichTextBox.InvokeRequired)
    24             {
    25                 lock (iAccumulator)
    26                 {
    27                     iAccumulator += aChar;
    28                 }
    29                 
    30                 //WriteDelegate d = new WriteDelegate(Write);
    31                 //iRichTextBox.Invoke(d, new object[] { aChar });
    32             }
    33             else
    34             {
    35                 Flush();
    36                 iRichTextBox.AppendText(aChar.ToString()); // When character data is written, append it to the text box.
    37             }            
    38         }
    39 
    40         public override Encoding Encoding
    41         {
    42             get { return System.Text.Encoding.UTF8; }
    43         }
    44 
    45         public void FlushAccumulator()
    46         {
    47             lock (iAccumulator)
    48             {
    49                 if (!string.IsNullOrEmpty(iAccumulator))
    50                 {
    51                     iRichTextBox.AppendText(iAccumulator); // When character data is written, append it to the text box.
    52                     iAccumulator = "";
    53                 }
    54 
    55             }
    56 
    57         }
    58     }
    59 }