首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DoWork事件的WPF后台调用方法

DoWork事件的WPF后台调用方法
EN

Stack Overflow用户
提问于 2013-10-31 04:18:05
回答 3查看 173关注 0票数 0

我的WPF UserControl上有一个UserControl。

代码语言:javascript
复制
        private readonly BackgroundWorker worker = new BackgroundWorker();

        public ucCustomer()
        {
          InitializeComponent();
          worker.DoWork += worker_DoWork;
          worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        }
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // run all background tasks here
            System.Threading.Thread.Sleep(10000);
        }

        private void worker_RunWorkerCompleted(object sender,  RunWorkerCompletedEventArgs e)
        {
            //update ui once worker complete his work
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            worker.RunWorkerAsync();
        }

上面的代码是工作的,UI是任务工作时的响应,但是如果我将worker_DoWork()更改为

代码语言:javascript
复制
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // run all background tasks here
   Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, 
        new Action(() => {
        gridDataBind(); //A long data-mining task,using Dispatcher.Invoke() to access UI.
    }));
} 

private void gridDataBind()
{
    SnEntities sn = new SnEntities();
    var customers = from c in sn.Customer select c;
    dgCustomer.ItemsSource = customers.ToList();
}

UI被冻结,直到任务结束。

有什么解决办法吗?谢谢你。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-31 04:33:07

尝试按下面的代码设置ItemsSource:

代码语言:javascript
复制
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // run all background tasks here
    e.Result = gridDataBind(); //A long data-mining task.
}

private IList<Customer> gridDataBind()
{
    SnEntities sn = new SnEntities();
    var customers = from c in sn.Customer select c;
    return customers.ToList();
}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        var customers = e.Result as IList<Customer>;

        ObservableCollection<Customer> gridItemsSource = new ObservableCollection<Customer>();
        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
                             new Action(() =>
                             {
                                 dgCustomer.ItemsSource = gridItemsSource;
                             }));

        foreach(var customer in customers)
        {
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
                                 new Action(() =>
                                 {
                                     gridItemsSource.Add(customer);
                                 }));
        }

    }
票数 1
EN

Stack Overflow用户

发布于 2013-10-31 05:01:13

将数据存储在e.result中的worker_DoWork中,并在worker_RunWorkerCompleted中更新UI。在这种情况下,当数据来自数据库时,UI将是免费的。

票数 0
EN

Stack Overflow用户

发布于 2013-10-31 11:43:38

试试这个,它会帮你的

代码语言:javascript
复制
Application.Current.Dispatcher.BeginInvoke(
  DispatcherPriority.Background,
  new Action(() => gridDataBind();));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19698361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档