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))));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值