这样一连串的动作而不会阻塞UI线程 [英] Doing series of actions without blocking UI Thread

查看:153
本文介绍了这样一连串的动作而不会阻塞UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的一系列动作,同时依赖异常扔还是不抛出任何异常更新我的UI,我怎么能做到这样呢?

I want to do a series of actions and simultaneously depend on exception it throw or doesn't throw any exception update my UI, how can I achieve something like this?

我已经试图使新的线程,每个步骤,但它并没有等到一个步骤结束,并将其写入总是失败的消息,使用的Thread.join()没有帮助可能是因为它阻止UI线程太..

I've already tried making new thread for each step but it doesn't wait until one of steps ends and it writes fail message always, using Thread.Join() didn't help either because it blocks UI thread too...

推荐答案

您需要的 BackgroundWorker的。把你想在后台线程来执行代码的DoWork 处理程序和更新UI的代码 RunWorkerCompleted 事件处理程序。

You need BackgroundWorker. Put the code you want to execute in background thread in DoWork handler and code of updating UI in RunWorkerCompleted event handler.

如果你要更新从的DoWork 处理程序之间的界面,你可以排队它UI调度这样的 -

In case you want to update GUI in between from DoWork handler, you can queue it on UI dispatcher like this -

App.Current.Dispatcher.BeginInvoke((Action)delegate
        {
           // Update GUI here.
        }, null);

这篇关于这样一连串的动作而不会阻塞UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆