Correct postthreadmessage usage

Source: Internet
Author: User

Starts a thread, creates a modelness dialog box with wtl, and then creates a message loop.

unsigned WINAPI MainDlg::ShowTipThread(LPVOID lpParameter){MainDlg* pThis = (MainDlg*) lpParameter;CMessageLoop theLoop;TipDlg theTipDlg;theTipDlg.Create(pThis->m_hWnd);theTipDlg.StartWorking();theLoop.Run();return 0;}

The dialog box can work, but it needs to communicate with the main interface thread,

Therefore, it is natural to use postthreadmessage.

void MainDlg::OnAction(){::PostThreadMessage(m_uThreadTip, WM_MY_MSG, 0, 0);}
At this time, we find that thetipdlg cannot receive the custom message at all. However, if: postmessage (m_uthreadtip, wm_quit,), The subwindow can receive the message and destroy

I checked the msdn and found that I had the correct meaning. I mistakenly thought that the message sent to the thread using postmessage would be distributed to the window created by this thread.


1. Use postmessage to send messages to a thread. This thread must have a message queue created by the system for the thread. Calling peekmessage or getmessage will force the system to create a message queue for the thread.

2. The message can be received through a message loop. However, the window handle is not specified for the postmessage message, which will be filtered out.

The: dispatchmessage (& m_msg) is discarded, and wm_quit is special and will be processed before this.

3. A message queue is created by the system, and a message loop is created by a thread. A thread can create multiple windows,





Correct postthreadmessage usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.