为什么必须始终从UI线程创建/更新UI元素? [英] Why must UI elements always be created/updated from the UI thread?

查看:65
本文介绍了为什么必须始终从UI线程创建/更新UI元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么必须始终从UI线程创建/更新UI元素?

Why must UI elements always be created/updated from the UI thread?

(几乎?)所有编程语言的UI元素都只能从UI线程安全地访问/修改.我了解这是一个标准的并发访问和同步问题,但确实有必要吗?这种行为是由编程语言还是由操作系统强加的?是否有任何一种编程语言在这种情况下有所不同?

In (almost?) all programming languages UI elements may be safely accessed/modified only from the UI thread. I understand that it's a standard concurrent access and synchronization problem, but is it really necessary? Is this behaviour imposed by the programming languages or by the operating system? Are there any programming languages where this situation is different?

推荐答案

它是由图形框架强加的-它通常(但并非总是)由操作系统提供.

It's imposed by the graphics framework - which is often (but not always) supplied by the operating system.

基本上,将所有内容设置为适当的线程安全"是无效的.虽然不得不封送回UI线程的确很痛苦,但它允许UI线程本身非常快速地处理事件,而不必担心锁定等.

Basically, making everything "properly threadsafe" is inefficient. While it's certainly a pain to have to marshal calls back to the UI thread, it allows the UI thread itself to process events extremely quickly without having to worry about locking etc.

这篇关于为什么必须始终从UI线程创建/更新UI元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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