Http://msdn.microsoft.com/en-us/library/ms644946 (V = vs.85). aspx
A message queue must be created for the thread that can receive messages (that is, the thread to deliver messages to). Otherwise, a message queue cannot be sent to the postthreadmessage. You can use the following method to handle this situation.
First (in the source thread) Create an event object and then create a new thread
Before calling postthreadmessage (source thread), use the waitforsingleobject function to wait for the event until it is set to the excitation state (target shipping thread ).
In the target shipping thread, call peekmessage as follows to force the system to create a message queue for this thread:
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
In the target shipping thread, set the event to the excitation state, indicating that it is ready to receive the shipping message.
In the target shipping thread, you can call the getmessage or peekmessage function to retrieve the delivered message. The hwnd member value in the MSG structure returned by the function is null.
Messages sent by postthreadmessage are not associated with a window (object handle. Generally, messages not associated with a window (object handle) cannot be distributed by the dispatchmessage function. Therefore, if the recipient thread is in a modal loop (such as using MessageBox or dialogbox), the message will be lost. In this case, a thread-specific Hook can be used to intercept thread messages.
The following is an example.
From: http://www.cppblog.com/sandy/archive/2012/09/20/2320.html
# Include <windows. h>
# Include <cstdio>
# Include <process. h>
# Define my_msg wm_user+ 100
Const int max_info_size = 20;
Handle hstartevent; // thread start event
// Thread function
Unsigned _ stdcall fun (void * PARAM)
{
Printf ("thread Fun start \ n ");
MSG;
Peekmessage (& MSG, null, wm_user, wm_user, pm_noremove );
If (! Setevent (hstartevent) // set thread start event
{
Printf ("set start event failed, errno: % d \ n",: getlasterror ());
Return 1;
}
While (true)
{
If (getmessage (& MSG, 0, 0) // get MSG from Message Queue
{
Switch (msg. Message)
{
Case my_msg:
Char * pinfo = (char *) msg. wparam;
Printf ("Recv % s \ n", pinfo );
Delete [] pinfo;
Break;
}
}
};
Return 0;
}
Int main ()
{
Handle hthread;
Unsigned nthreadid;
Hstartevent =: createevent (0, false, false, 0); // create thread start event
If (hstartevent = 0)
{
Printf ("create start event failed, errno: % d \ n",: getlasterror ());
Return 1;
}
// Start thread
Hthread = (handle) _ beginthreadex (null, 0, & fun, null, 0, & nthreadid );
If (hthread = 0)
{
Printf ("start thread failed, errno: % d \ n",: getlasterror ());
Closehandle (hstartevent );
Return 1;
}
// Wait thread start event to avoid postthreadmessage return errno: 1444
: Waitforsingleobject (hstartevent, infinite );
Closehandle (hstartevent );
Int COUNT = 0;
While (true)
{
Char * pinfo = new char [max_info_size]; // create dynamic msg
Sprintf (pinfo, "MSG _ % d", ++ count );
If (! Postthreadmessage (nthreadid, my_msg, (wparam) pinfo, 0) // post thread msg
{
Printf ("post Message failed, errno: % d \ n",: getlasterror ());
Delete [] pinfo;
}
: Sleep (1000 );
}
Closehandle (hthread );
Return 0;
}