WPF 异步编程需要注意的点

先贴一段代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;

namespace WpfFileSearch
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        FileSearchHelper fileSearch;

        public MainWindow()
        {
            InitializeComponent();
            fileSearch = new FileSearchHelper();
            fileSearch.init();
        }

        private void FileRange_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == true)
            {
                string[] fileNames = ofd.FileNames;
                if (null != fileSearch)
                {
                    fileSearch.setFilteredFiles(fileNames);
                    DGFilteredFiles.ItemsSource = new ObservableCollection<string>(fileSearch.getFilteredFiles());
                }
            }
        }

        private void searchJob(string searchString)
        {
            if (!string.IsNullOrEmpty(searchString))
            {
                fileSearch.setSearchString(searchString);
                fileSearch.fileSearch();
            }
            this.Dispatcher.BeginInvoke(new Action(() => DGMatchedFiles.ItemsSource = new ObservableCollection<string>(fileSearch.getResult())));
            this.Dispatcher.BeginInvoke(new Action(() => FileRange.IsEnabled = true));
            this.Dispatcher.BeginInvoke(new Action(() => Search.IsEnabled = true));
            this.Dispatcher.BeginInvoke(new Action(() => ImgStatus.Source = new BitmapImage(new Uri("res/BitmapDone.bmp", UriKind.Relative))));
            MessageBox.Show("results counts: " + fileSearch.getResult().Count().ToString());
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            FileRange.IsEnabled = false;
            Search.IsEnabled = false;
            try
            {
                ImgStatus.Source = new BitmapImage(new Uri("res/BitmapWait.bmp", UriKind.Relative));
                string searchString = SearchString.Text.ToString();
                Task task = Task.Factory.StartNew(() => searchJob(searchString));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

功能比较简单,实现的是文件查找功能,前台UI显示的效果图如下:
这里写图片描述

主要需要注意的是这儿:
Task task = Task.Factory.StartNew(() => searchJob(searchString));
起了一个新的线程(searchJob)之后,新的线程在文件查找结束之后需要改变前台UI信息。如果在该线程(searchJob)中这么写:

private void searchJob(string searchString)
        {
            if (!string.IsNullOrEmpty(searchString))
            {
                fileSearch.setSearchString(searchString);
                fileSearch.fileSearch();
            }
            DGMatchedFiles.ItemsSource = new ObservableCollection<string>(fileSearch.getResult());
            FileRange.IsEnabled = true;
            Search.IsEnabled = true;
            ImgStatus.Source = new BitmapImage(new Uri("res/BitmapDone.bmp", UriKind.Relative));
            MessageBox.Show("results counts: " + fileSearch.getResult().Count().ToString());
        }

我们会得到一个error:
An exception of type ‘System.InvalidOperationException’ occurred in WindowsBase.dll but was not handled in user code

Additional information: The calling thread cannot access this object because a different thread owns it.
这是因为WPF的线程模型决定的,WPF的线程模型规定UI必须由创建它的线程去控制,所以导致了这个问题,正确的解决方法应该是使用this.Dispatcher.BeginInvoke,即:

            this.Dispatcher.BeginInvoke(new Action(() => DGMatchedFiles.ItemsSource = new ObservableCollection<string>(fileSearch.getResult())));
            this.Dispatcher.BeginInvoke(new Action(() => FileRange.IsEnabled = true));
            this.Dispatcher.BeginInvoke(new Action(() => Search.IsEnabled = true));
            this.Dispatcher.BeginInvoke(new Action(() => ImgStatus.Source = new BitmapImage(new Uri("res/BitmapDone.bmp", UriKind.Relative))));

猜你喜欢

转载自blog.csdn.net/apollo1shine/article/details/78668033
WPF