C# 如何在不冻结屏幕的情况下验证RichTextBox中的大文本?

C# 如何在不冻结屏幕的情况下验证RichTextBox中的大文本?,c#,wpf,multithreading,async-await,nhunspell,C#,Wpf,Multithreading,Async Await,Nhunspell,我目前正在使用NHunspell在WPF中实现自定义拼写检查,因为.Net Framework的本机解决方案不适合我的需要。但是我在检查大文本中的单词时遇到了问题,例如带有10个字母的Lorem Ipsum,因为我需要检查每个单词,看看它是否包含在拼写使用的词典中,如果没有,我需要在该单词下面划线 我有一个当前的方法,每次KeyUp是退格键或空格键时都检查所有文本 var textRange = new TextRange(SpellCheckRichTextBox.Document.Conte

我目前正在使用NHunspell在WPF中实现自定义拼写检查,因为.Net Framework的本机解决方案不适合我的需要。但是我在检查大文本中的单词时遇到了问题,例如带有10个字母的Lorem Ipsum,因为我需要检查每个单词,看看它是否包含在拼写使用的词典中,如果没有,我需要在该单词下面划线

我有一个当前的方法,每次KeyUp是退格键或空格键时都检查所有文本

var textRange = new TextRange(SpellCheckRichTextBox.Document.ContentStart, 
SpellCheckRichTextBox.Document.ContentEnd);
        textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
        _viewModel.Text = textRange.Text;
        var zzz = _viewModel.Text.Split(' ');
        var kfeofe = zzz.Where(x => _viewModel.MisspelledWords.Contains(x));
        foreach (var item in kfeofe)
        {
            TextPointer current = textRange.Start.GetInsertionPosition(LogicalDirection.Forward);

            while (current != null)
            {
                string textInRun = current.GetTextInRun(LogicalDirection.Forward);
                if (!string.IsNullOrWhiteSpace(textInRun))
                {
                    int index = textInRun.IndexOf(item.ToString());
                    if (index != -1)
                    {
                        TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
                        TextPointer selectionEnd = selectionStart.GetPositionAtOffset(item.ToString().Length, LogicalDirection.Forward);
                        TextRange selection = new TextRange(selectionStart, selectionEnd);

                        selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
                    }
                }
                current = current.GetNextContextPosition(LogicalDirection.Forward);
            }

        }
但是,我认为我需要一个异步解决方案,这样它就不会阻塞我的主线程和用户的输入。 -理论上,如果用户花费2秒钟以上没有输入,然后将选中的TextRange返回到我的RichTextBox(SpellCheckRichTextBox),我会考虑运行一个并行线程

有没有人能提出一些解决方案,这样我在处理大文本时,验证速度就不会那么慢了?我真的被困在那里了,任何帮助都将不胜感激。
提前谢谢

第一个改进是

zzz.AsParallel().Where(x => _viewModel.MisspelledWords.Contains(x)).ToList();
这显然是假设您的
.MisspelledWords.Contains(x)
是可以并行完成的。这可能已经是一场灾难了

事实上,你有一个拼错的单词集合,让我相信你已经分析了一次文本。那么为什么要对它进行两次解析呢?你为什么不能把这两个过程结合起来呢?这将是另一种可能的优化

是的,当用户停止键入时,在另一个线程中执行所有这些操作会更好