Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从后台线程访问WPF控件_C#_.net_Wpf_Multithreading - Fatal编程技术网

C# 如何从后台线程访问WPF控件

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

我有一个RichTextBox,我试图查找并突出显示与用户提供的查询匹配的所有单词。我的代码可以工作,但是对于相当大的文档,它会挂起UI,因为一切都是在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}));
列出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}));