MFC:从另一个线程调用CWnd方法是否安全? [英] MFC: Is it safe to call CWnd methods from another thread?

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

问题描述

实际上我有两个问题:

  1. 从工作线程中调用SendMessage是否安全?
  2. CWnd方法(如MessageBox)是否在后台调用API函数SendMessage?
  1. Is it safe to call SendMessage from a worker thread?
  2. Do CWnd methods, like MessageBox, call API function SendMessage behind the scene?

据我了解,当工作线程调用SendMessage时,它将消息推送到UI线程的消息队列中,并等待直到该消息被处理为止.在这种情况下,这样做是安全的.

Per my understanding, when the worker thread calls SendMessage, it pushes the message into the message queue of the UI thread, and waits until this message is processed. In that case, it would be safe to do so.

对此我不太确定.如果我错了,请纠正我.

I'm not quite sure about this. please correct me if I was wrong.

非常感谢.

------------------------更新---------------------- ------------

------------------------ update ----------------------------------

结论:

  • 跨线程调用Windows API ::SendMessage::PostMessage是安全的.
  • 跨线程调用CWnd方法并不安全.某些方法可能是安全的,但不能保证.
  • It's safe to call the windows API ::SendMessage and ::PostMessage across threads.
  • It's not safe to call CWnd methods across threads. Some of the methods may be safe, but it's not guaranteed.

非常感谢大家.

推荐答案

从工作线程中调用SendMessage是否安全?

是的.系统确保消息处理在接收线程上被序列化.当跨线程发送消息时,将阻止发件人,直到处理完消息为止.接收器仅在执行消息检索代码(GetMessagePeekMessage等)时才处理跨线程发送的消息.已发送的消息永远不会在消息队列中排队. SendMessage 的文档具有其他详细信息.

Yes. The system makes sure, that message handling is serialized on the receiving thread. When sending messages across threads, the sender is blocked until the message has been handled. The receiver only handles a cross-thread sent message when it executes message retrieval code (GetMessage, PeekMessage, etc.). Sent messages are never queued in the message queue. The documentation for SendMessage has additional details.

CWnd方法(如MessageBox)是否在后台调用API函数SendMessage?

Do CWnd methods, like MessageBox, call API function SendMessage behind the scene?

是的.首先,消息框将接收诸如WM_CREATEWM_NCCREATE之类的标准窗口消息,作为对话框结构的一部分.同样,对于自有的窗口(如模式对话框),系统将同时发送WM_ACTIVATE消息到正在禁用的窗口和正在激活的窗口.我不确定这为什么重要,或者您为什么特别问这个问题.

Yes. For one, the message box will receive standard window messages like WM_CREATE or WM_NCCREATE as part of the dialog construction. Also, for owned windows (like modal dialogs), the system will send WM_ACTIVATE messages to both the window being deactivated, and the window being activated. I'm not sure why this matters, though, or why you asked this question in particular.

现在在标题中提问:

从另一个线程调用CWnd方法是否安全?

通常,不会.不过,它确实取决于成员.有些可以安全呼叫,有些则不能.特别是,所有修改窗口状态(内容,可见性,激活等)的方法都应仅从创建窗口的线程中调用.万一呼叫不安全,系统仍将保持一致状态.但是,您的应用程序可能不是.

In general, no. It does depend on the member, though. Some are safe to call, others aren't. In particular, all methods that modify window state (contents, visibility, activation, etc.) should only be called from the thread that created the window. In case the call is not safe, the system will still be in a consistent state. However, your application may not be.

这篇关于MFC:从另一个线程调用CWnd方法是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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