简体   繁体   English

如何提高RichTextBox日志写入性能?

[英]How To Improve RichTextBox Log Write Performance?

I made a Log Window with colored text using a RichTextBox . 我使用RichTextBox创建了一个带有彩色文本的日志窗口。

It runs hidden Hide() at start until you press a button to bring it up Show() . 它在开始时运行隐藏的Hide() ,直到您按下按钮将其调出Show()为止。

I was using a TextBlock before , but you couldn't select text, so I switched to RichTextBox . 我以前使用过TextBlock before ,但是您无法选择文本,所以我切换到RichTextBox It has increased the finish time by 5 seconds when before it was almost instant. 几乎在瞬间之前,完成时间增加了5秒。

How can I improve write speed or defer writing all messages until the end of the program? 如何提高写入速度或将所有消息推迟写入程序的末尾?

Throughout the program I write to the log many times using: 在整个程序中,我使用以下命令多次写入日志:

public partial class MainWindow : Window
{
    public Paragraph paragraph = new Paragraph();  

    public MainWindow()
    {
        InitializeComponent();
    }

    public void MyMethod() {
        rtbLog.Document = new FlowDocument(paragraph);
        paragraph.Inlines.Add(new LineBreak());
        paragraph.Inlines.Add(new Bold(new Run("Log Message Example")) { Foreground = Brushes.Blue });
        paragraph.Inlines.Add(new LineBreak());   
        paragraph.Inlines.Add(new Run("Log Message Example 2") { Foreground = Brushes.White });   
        this.DataContext = this;
    }
}

<RichTextBox x:Name="rtbLog" HorizontalAlignment="Left" VerticalAlignment="Top"
             Width="600" Height="400" Margin="246,10,0,0" Padding="10"
             FontFamily="Consolas" Background="#FF000000"
             Foreground="White" VerticalScrollBarVisibility="Auto"
             IsReadOnly="True" IsUndoEnabled="False"/>

To improve speed I've tried using IsUndoEnabled="False" and TextOptions.TextFormattingMode="Display" , it might have reduced the time by 1 second. 为了提高速度,我尝试使用IsUndoEnabled="False"TextOptions.TextFormattingMode="Display" ,它可能会将时间减少了1秒。

日志控制台

I would keep a list of all the log events around outside of both of the windows and let both class access them so some static class: 我将在两个窗口之外保留所有日志事件的列表,并让两个类都访问它们,以便使用一些静态类:

public static class Logs
{
    public static Paragraph Paragraph { get; set; }

    static Logs()
    {
        Paragraph = new Paragraph();
    }
}

Then when you need to write to the logs do something like the following: 然后,当您需要写入日志时,请执行以下操作:

public void MyMethod()
{
    Logs.Paragraph.Inlines.Add(new LineBreak());
    Logs.Paragraph.Inlines.Add(new Bold(new Run("Log Message Example")) { Foreground = Brushes.Blue });
}

Then when you want to show the window call: 然后,当您想显示窗口调用时:

SomeWindow someWindow = new SomeWindow();
someWindow.ShowDialog(); //Or show

Then you need a new window class with the RichTextBox on it and add the paragraph on start. 然后,您需要一个带有RichTextBox的新窗口类,并在开始时添加该段落。

public class SomeWindow : Window
{
    public SomeWindow()
    {
        rtbLog.Document = new FlowDocument(Logs.Paragraph);
    }
}

I was able to solve write performance by adjusting the code. 我能够通过调整代码来解决写性能。

One problem was using rtbLog.Document = new FlowDocument(paragraph) multiple times. 一个问题是多次使用rtbLog.Document = new FlowDocument(paragraph)。

  • Put multiple Inline.Add()'s in a List of Actions. 将多个Inline.Add()放在动作列表中。

  • Create Method that writes all Actions to RichTextBox using foreach loop with BeginChange() & EndChange(). 创建方法,使用带有BeginChange()和EndChange()的foreach循环将所有操作写入RichTextBox。

  • Optionally put Method in BackgroundWorker. (可选)将Method放入BackgroundWorker。

Speed is now instant. 现在速度很快。

List<Action> LogActions = new List<Action>();
Action WriteAction;


// Create Multiple Actions 
WriteAction = () =>
{
    paragraph.Inlines.Add(new LineBreak());
    paragraph.Inlines.Add(new Bold(new Run("Example")) { Foreground = Brushes.White });
};
// Add Actions to List
LogActions.Add(WriteAction);


// At End of Program or in a Method:

public void LogWriteAll() {
    // Write All Actions in List
    myWindow.rtbLog.Document = new FlowDocument(paragraph);

    // Begin Rich Textbox Change
    myWindow.rtbLog.BeginChange();

    foreach (Action Write in LogActions)
    {
        Write();
    }

    myWindow.rtbLog.EndChange();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM