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

查看:14
本文介绍了为什么必须始终从 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天全站免登陆