如何从另一个线程通知 CDialog 对象已完成的任务?

发布于 2024-10-28 16:30:25 字数 355 浏览 2 评论 0原文

我有一个长时间运行的任务和一个对话框,通知用户该任务正在运行。任务完成后,对话框会通知用户。

我想在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-11-04 16:30:25

您可以使用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:

Places (posts) a message in the
message queue associated with the
thread that created the specified
window and returns without waiting for
the thread to process the message.

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.

偏爱你一生 2024-11-04 16:30:25

PostMessage()SendMessage() 都以异步或同步方式将消息传递到窗口句柄。

窗口句柄具有线程关联性。这意味着与窗口句柄交互的任何代码都必须从拥有该窗口的线程(即创建该窗口的线程)运行。

PostMessage()SendMessage() 通过确保处理消息时由拥有该窗口的线程处理该消息来处理此问题。

对于 PostMessage() 来说,这是一个简单的任务。每个线程都有自己的个人消息队列。当您调用PostMessage()时,系统只是将消息放置在属于拥有该窗口的线程的消息队列中。然后,当线程泵送其消息队列时,该消息将在稍后的某个时间点进行处理。

对于 SendMessage() 来说,安排消息由正确的线程处理是更困难的。如果您从拥有该窗口的线程调用 SendMessage(),则直接调用窗口过程。否则,系统会通知另一个线程需要运行同步消息,然后阻塞。另一个线程(拥有该窗口的线程)仅在发出某些系统调用来检测消息正在等待这一事实时才处理该消息。这意味着对 SendMessage() 的跨线程调用可能会导致性能问题。

Both PostMessage() and SendMessage() 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() and SendMessage() 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 call PostMessage() 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 call SendMessage() 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 to SendMessage() can lead to performance problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文