如何从另一个线程通知 CDialog 对象已完成的任务?
我有一个长时间运行的任务和一个对话框,通知用户该任务正在运行。任务完成后,对话框会通知用户。
我想在使用 AfxBeginThread
创建的工作线程中启动任务,当任务完成时,我使用 PostMessage
发布用户消息 WM_APP + 1
代码> 到对话框。显然 PostMessage
只能在同一个线程中使用,因此我尝试了 PostThreadMessage
但在对话框中使用 ON_THREAD_MESSAGE
时出现了编译器错误。
现在我不知道如何继续。您有什么建议吗?
谢谢!
I have a long running task and a Dialog which informs the User that this task is running. When the task has finished, the Dialog informs the User about it.
I thought to start the task within a Worker-Thread created with AfxBeginThread
and when the task has finished I post a user-message WM_APP + 1
with PostMessage
to the Dialog. Apperently PostMessage
could only be used within the same thread, thus I tried PostThreadMessage
but I got compiler errors when using ON_THREAD_MESSAGE
within my Dialog.
Now I don't know how to continue. Do you have any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
PostMessage()
,它会起作用。 GUI 线程将处理该消息。我相信 PostMessage 的文档是清除:
因此,如果工作人员将消息放入窗口的消息队列中,
创建窗口的线程将处理该消息。
在你的情况下,它是主(或GUI)线程。
You can use
PostMessage()
, it'll work. The gui thread will process that message.I believe the documentation of PostMessage is clear:
So, if the worker places a message in the message queue of a window,
the thread which created the window will process the message.
In your case it is the main (or gui) thread.
PostMessage()
和SendMessage()
都以异步或同步方式将消息传递到窗口句柄。窗口句柄具有线程关联性。这意味着与窗口句柄交互的任何代码都必须从拥有该窗口的线程(即创建该窗口的线程)运行。
PostMessage()
和SendMessage()
通过确保处理消息时由拥有该窗口的线程处理该消息来处理此问题。对于
PostMessage()
来说,这是一个简单的任务。每个线程都有自己的个人消息队列。当您调用PostMessage()
时,系统只是将消息放置在属于拥有该窗口的线程的消息队列中。然后,当线程泵送其消息队列时,该消息将在稍后的某个时间点进行处理。对于
SendMessage()
来说,安排消息由正确的线程处理是更困难的。如果您从拥有该窗口的线程调用SendMessage()
,则直接调用窗口过程。否则,系统会通知另一个线程需要运行同步消息,然后阻塞。另一个线程(拥有该窗口的线程)仅在发出某些系统调用来检测消息正在等待这一事实时才处理该消息。这意味着对SendMessage()
的跨线程调用可能会导致性能问题。Both
PostMessage()
andSendMessage()
deliver messages to window handles, asynchronously or synchronously.Window handles have thread affinity. That means that any code that interacts with a window handle must be run from the thread that owns the window, that is the thread that created the window.
PostMessage()
andSendMessage()
deal with this by ensuring that when the message is processed, it is processed by the thread that owns the window.For
PostMessage()
it is a simple task. Each thread has its own personal message queue. When you callPostMessage()
the system simply places the message on the message queue belonging to the thread which owns the window. The message is then processed at some later point in time when the thread pumps its message queue.For
SendMessage()
it is more difficult to arrange that the message is handled by the right thread. If you callSendMessage()
from the thread that owns the window then the window procedure is called directly. Otherwise the system notifies the other thread that a synchronous message needs to run and then blocks. The other thread, the one that owns the window, only processes the message when it makes certain system calls that detect the fact that a message is waiting. This means that cross-thread calls toSendMessage()
can lead to performance problems.