问题描述
在WPF中反序列化大量UI元素时,要防止UI冻结,我有什么解决方案?当我尝试将对象加载到另一个线程中时,出现错误抱怨:对象属于UI线程.因此,在加载UI数据时,我必须采取哪些措施来防止Vista程序无响应"错误?我可以依靠单线程解决方案,还是缺少有关多个UI线程的信息?
What solutions do I have if I want to prevent the UI from freezing while I deserialize a large number of UI elements in WPF? I'm getting errors complainig that the objects belong on the UI Thread when I'm trying to load them in another thread. So, what options do I have to prevent the Vista "Program not responding" error while I'm loading my UI data? Can I rely on a single-threaded solution, or am I missing something regarding perhaps multiple UI Threads?
推荐答案
如果仅使用单个线程,则在进行任何数量的处理时UI都会冻结.
If you only use a single thread then the UI will freeze while you do any amount of processing.
如果您使用BackgroundWorker线程,则可以更好地控制发生的情况&什么时候.
If you use a BackgroundWorker thread you'll have more control over what happens & when.
要更新UI,您需要使用 Dispatcher.Invoke
从您的后台线程中调出跨越线程边界的调用.
To update the UI you need to use Dispatcher.Invoke
from your background thread to marshal the call across the thread boundary.
Dispatcher.Invoke(DispatcherPriority.Background,
new Action(() => this.TextBlock.Text = "Processing");
这篇关于防止UI冻结而无需其他线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!