About postthreadmessage and creating a thread Message Queue

Source: Internet
Author: User

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.

  1. First (in the source thread) Create an event object and then create a new thread

  2. 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 ).

  3. 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)

  4. 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;
}

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.