Use postthreadmessage to send messages to the working thread

Source: Internet
Author: User

I read the messages sent between the window threads carefully and felt that the previous understanding was not profound. Let's talk about postthreadmessage.

Postthreadmessage is a thread body that sends a message to the specified Thread ID. Its prototype is as follows: bool postthreadmessage (DWORD idthread, uint MSG, wparam, lparam ); this function can send messages to the worker thread or the UI thread. The thread that accepts postthreadmessage must already have a message queue; otherwise, calling postthreadmessage will fail. If getlasterror is used for this reason, the error code 1444 is returned. In this case, there are two solutions:

1. Call postthreadmessage. If the call fails, sleep will call postthreadmessage again for a period of time until the call is successful;

2. Create an event object so that a message queue can be created for a thread whose postthreadmessage is waiting for acceptance. You can call peekmessage to force the system to create a message queue. ExampleCodeAs follows:

Assume that mainapp is the sending thread and threada is the receiving thread/* mainapp. cpp */......

Hstartevent =: createevent (0, false, false, 0); // create thread start event

If (hstartevent = 0 ){

Printf ("create start event failed, errno: % d \ n ",

: Getlasterror ());

Return 1;

}

: Waitforsingleobject (hstartevent, infinite );

Closehandle (hstartevent );

If (! Postthreadmessage (threadaid, wm_message_a, 0, 0 )){

_ Tprintf (_ T ("post error! % D \ n "), getlasterror ());

Return 1;

}

...... Threada is the acceptance thread/* threada */

MSG;

Peekmessage (& MSG, null, wm_user, wm_user, pm_noremove );

If (! Setevent (hstartevent )){

Printf ("set event error, % d \ n", getlasterror ());

Return 1;

}

While (true ){

If (getmessage (& MSG, 0, 0 )){

Switch (msg. Message ){

Case wm_message_a:

......

Break;

}}}}

// If the message passed by postthreadmessage needs to contain information, you must release the information in the message at the end. The method for attaching information to a message is as follows/* The construction information is as follows */char * pinfo = new char [max_info_size]; // create dynamic msg

Sprintf (pinfo, "MSG _ % d", ++ count );

Postthreadmessage (nthreadid, my_msg, (wparam) pinfo, 0) // post thread msg

/* The explanation is as follows */

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; // The resource Break is released here;

}}

 

A simple message communication experiment was conducted to let the main thread wait for user input, generate different messages, and post these messages to the sub-thread, the subthread generates different responses based on the messages generated. These subthreads can be working threads or UI threads.

# Include

# Include

# Include

# 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.