在 C# 中向具有不同颜色的 richtextbox 添加新行

adding new line to richtextbox with different color in c#

我创建了一个程序,它向 cmd 发送命令,从 cmd 获取输出并将其显示在带有颜色的 richtextbox 中。 我写了这个函数:

           System.Windows.Documents.TextRange rangeOfText =
                new System.Windows.Documents.TextRange(this.Console.Document.ContentEnd,
                    this.Console.Document.ContentEnd)
                { Text = line + "\n" };
            rangeOfText.ApplyPropertyValue(TextElement.ForegroundProperty, color);

this.Console 是我的富文本框。

问题是我从 cmd 得到了很多行,所以 UI 被阻塞了很多。 还有比这更有效的吗?

如果您在排序时 运行 太多命令,那么您的 UI 肯定会卡住。您必须创建单独的线程来读取文件和更新 RichTextBox。做这个。 1. Crete 一个线程读取文件。放置间隔100ms。 2. 一旦收到数据更新RichTextBox。您的新线程无法更新 RichTExtBOx,因此您必须使用 delegate 来更新 RTB。像“richTetxbox1.Invoke