MFC:从另一个线程访问GUI? [英] MFC: accessing GUI from another thread?

查看:119
本文介绍了MFC:从另一个线程访问GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以一般只有主线程应该在MFC应用程序中访问GUI。



但是是一个法律还是只是推荐?如果我确保,通过关键部分,只有一个线程访问GUI中的某个对象,是否确定呢?或者是一个问题,如果MAIN线程访问GUI的一部分,而另一个线程访问一个。即使这两个对象彼此不影响吗?



我问的原因是因为这简化了我的应用程序的重写,如果我可以从另一个线程。

解决方案

不要这样做。你会生活在一个世界的ASSERTs和奇怪的行为,如果你这样做。 GUI通过在主线程上泵送的Windows消息系统工作。如果你开始在另一个线程中修改UI,你会遇到你的操作会导致其他UI消息的情况,这将由主线程处理,同时你仍然试图访问另一个线程上的UI。 p>

MFC编程是很困难的,没有尝试处理这种事情。而是使用PostMessage将UI相关的处理放到主线程上。


So generally only the main thread should access the GUI in a MFC application.

However is that a law or just recommended? If I make sure, via critical sections, that only one thread accesses a certain object in the GUI, is it ok then? Or is it a problem if the MAIN thread accesses one part of the GUI while another thread access one. Even if those 2 objects don't affect each other?

The reason I ask is because this simplifies my rewrite of the application a lot if I can access the GUI from another thread.

解决方案

Don't do it. You'll live in a world of ASSERTs and weird behaviour if you do. The GUI works through a system of Windows messages which are 'pumped' on the main thread. If you start modifying the UI in another thread you'll have situations where your operation causes other UI messages, which will be handled by the main thread potentially at the same time you're still trying to access the UI on another thread.

MFC programming is hard enough without trying to handle this sort of thing. Instead use PostMessage to put the UI related handling onto the main thread.

这篇关于MFC:从另一个线程访问GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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