为什么单线程模型,用于更新UI的主线程? [英] Why is a single threaded model used to update the UI as main thread?

查看:173
本文介绍了为什么单线程模型,用于更新UI的主线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Qt的医生说,

如前所述,每个程序都有一个线程启动时。本   线程被称为主线程(也被称为在GUI线程   Qt应用程序)。 Qt的图形用户界面必须在这个线程中运行。

As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread.

Android的医生说,

The Android doc says,

像的活动和其它成分,在主运行的服务   申请过程的线索

Like activities and the other components, services run in the main thread of the application process

和iOS的,

强烈建议不要从更新UI控件等   后台线程(例如定时器,通讯科等)。这可以是事业   崩溃它们有时很难辨认。相反,用这些   强制code将在UI线程上执行的(这是永远的   主线程)。

It is strongly recommended not to update UI controls etc from a background thread (e.g. a timer, comms etc). This can be the cause of crashes which are sometimes very hard to identify. Instead use these to force code to be executed on the UI thread (which is always the "main" thread).

为什么他们使用单线程模型进行更新的用户界面?

Why does they use a single threaded model to update UI ?

推荐答案

简短的回答是,这是唯一合理的方法,以确保显示器未损坏。

The short answer is, it's the only reasonable way to ensure that the display is not corrupted.

长的答复是,允许多个线程更新死锁,竞争条件和各种麻烦的UI效果。这是痛苦的教训教导Java的AWT(除其他UI系统),它允许多个线程触摸UI。见,例如,这个博客帖子

The long answer is that allowing multiple threads to update the UI results in deadlocks, race conditions, and all sorts of trouble. This was the painful lesson taught by Java's AWT (among other UI systems) that allows multiple threads to touch the UI. See, for instance, this blog post.

这篇关于为什么单线程模型,用于更新UI的主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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