我编写了一个代码来计算来自richtextbox 内容的行数和文本长度。使用小块文本,它可以完美运行。但是当我在richtextbox中按“Enter”或“Backspace”时有大块文本(超过100k)时,响应时间变得非常慢。例如:https ://i.imgur.com/QO2UrAw.gifv
我的问题。有什么更好的异步运行代码的方法?
使用测试项目存档 https://gofile.io/?c=LpF409
private void StatusPanelTextInfo()
{
int currentColumn = 0;
int currentLine = 0;
int linesCount = 0;
if (statusStrip1.Visible)
{
currentColumn = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
RichTextBox rtb = new RichTextBox
{
WordWrap = false,
Text = richTextBox1.Text
};
currentLine = 1 + rtb.GetLineFromCharIndex(richTextBox1.SelectionStart);
linesCount = richTextBox1.Lines.Count();
if (linesCount == 0)
{
linesCount = 1;
}
}
toolStripStatusLabel1.Text = "Length: " + richTextBox1.TextLength;
toolStripStatusLabel2.Text = "Lines: " + linesCount;
toolStripStatusLabel3.Text = "Ln: " + currentLine;
toolStripStatusLabel4.Text = "Col: " + currentColumn;
}