C# 如何从后台线程访问WPF控件
我有一个RichTextBox,我试图查找并突出显示与用户提供的查询匹配的所有单词。我的代码可以工作,但是对于相当大的文档,它会挂起UI,因为一切都是在UI线程上完成的C# 如何从后台线程访问WPF控件,c#,.net,wpf,multithreading,C#,.net,Wpf,Multithreading,我有一个RichTextBox,我试图查找并突出显示与用户提供的查询匹配的所有单词。我的代码可以工作,但是对于相当大的文档,它会挂起UI,因为一切都是在UI线程上完成的 List<TextRange> getAllMatchingRanges(String query) { TextRange searchRange = new TextRange(ricthBox.Document.ContentStart, ricthBox.Document.Conten
List<TextRange> getAllMatchingRanges(String query)
{
TextRange searchRange = new TextRange(ricthBox.Document.ContentStart, ricthBox.Document.ContentEnd);
int offset = 0, startIndex = 0;
List<TextRange> final = new List<TextRange>();
TextRange result = null;
while (startIndex <= searchRange.Text.LastIndexOf(query))
{
offset = searchRange.Text.IndexOf(query, startIndex);
if (offset < 0)
break;
}
for (TextPointer start = searchRange.Start.GetPositionAtOffset(offset); start != searchRange.End; start = start.GetPositionAtOffset(1))
{
if (start.GetPositionAtOffset(query.Length) == null)
break;
result = new TextRange(start, start.GetPositionAtOffset(query.Length));
if (result.Text == query)
{
break;
}
}
if (result == null)
{
break;
}
final.Add(result);
startIndex = offset + query.Length;
}
return final;
}
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action(() => {// Do your highlighting}));
列出getAllMatchingRanges(字符串查询)
{
TextRange searchRange=新的TextRange(ricthBox.Document.ContentStart,ricthBox.Document.ContentEnd);
int offset=0,startIndex=0;
最终列表=新列表();
TextRange结果=null;
while(startIndex一个选项是。让高亮显示在后台发生,而不阻塞UI线程
List<TextRange> getAllMatchingRanges(String query)
{
TextRange searchRange = new TextRange(ricthBox.Document.ContentStart, ricthBox.Document.ContentEnd);
int offset = 0, startIndex = 0;
List<TextRange> final = new List<TextRange>();
TextRange result = null;
while (startIndex <= searchRange.Text.LastIndexOf(query))
{
offset = searchRange.Text.IndexOf(query, startIndex);
if (offset < 0)
break;
}
for (TextPointer start = searchRange.Start.GetPositionAtOffset(offset); start != searchRange.End; start = start.GetPositionAtOffset(1))
{
if (start.GetPositionAtOffset(query.Length) == null)
break;
result = new TextRange(start, start.GetPositionAtOffset(query.Length));
if (result.Text == query)
{
break;
}
}
if (result == null)
{
break;
}
final.Add(result);
startIndex = offset + query.Length;
}
return final;
}
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action(() => {// Do your highlighting}));
一个选项是,让高亮显示在后台进行,而不阻塞UI线程
List<TextRange> getAllMatchingRanges(String query)
{
TextRange searchRange = new TextRange(ricthBox.Document.ContentStart, ricthBox.Document.ContentEnd);
int offset = 0, startIndex = 0;
List<TextRange> final = new List<TextRange>();
TextRange result = null;
while (startIndex <= searchRange.Text.LastIndexOf(query))
{
offset = searchRange.Text.IndexOf(query, startIndex);
if (offset < 0)
break;
}
for (TextPointer start = searchRange.Start.GetPositionAtOffset(offset); start != searchRange.End; start = start.GetPositionAtOffset(1))
{
if (start.GetPositionAtOffset(query.Length) == null)
break;
result = new TextRange(start, start.GetPositionAtOffset(query.Length));
if (result.Text == query)
{
break;
}
}
if (result == null)
{
break;
}
final.Add(result);
startIndex = offset + query.Length;
}
return final;
}
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action(() => {// Do your highlighting}));