为什么只有UI线程允许修改UI? [英] Why is only the UI thread allowed to modify the UI?

查看:137
本文介绍了为什么只有UI线程允许修改UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果我从不同的线程修改控制,我应该照顾,因为WinForms和WPF不允许其他线程修改控制的状态。

I know that if I am modifying a control from a different thread, I should take care because WinForms and WPF don't allow modifying control's state from other threads.

这是为什么限制的地方?

Why is this restriction in place?

如果我能写线程安全的code,我应该可以安全地修改控制状态。那么,为什么这样的限制present?

If I can write thread-safe code, I should be able to modify control state safely. Then why is this restriction present?

推荐答案

若干GUI框架有此限制。据该书的Java并发实践的这样做的原因是为了避免复杂的锁。问题是,GUI控制可能必须从UI两个事件,数据绑定等等,这导致来自几个不同的来源,因而死锁的危险锁定反应。为了避免这种情况的.NET的WinForms(和其他UI)限制访问组件到单个线程,从而避免锁定。

Several GUI frameworks have this limitation. According to the book Java Concurrency in Practice the reason for this is to avoid complex locking. The problem is that GUI controls may have to react to both events from the UI, data binding and so forth, which leads to locking from several different sources and thus a risk of deadlocks. To avoid this .NET WinForms (and other UIs) restricts access to components to a single thread and thus avoids locking.

这篇关于为什么只有UI线程允许修改UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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