开发者

How to inform a CDialog Object about a finished task from another thread?

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开发者_StackOverflow社区 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜