Reprint-use postthreadmessage to transmit messages between Win32 threads

Source: Internet
Author: User

Postthreadmessage

This function places a message in the message queue of the specified Thread
And then returns without waiting for the thread to process the message.

 
Bool Postthreadmessage (
DWORD Idthread,
Uint MSG,
Wparam Wparam,
Lparam Lparam
);
Parameters
Idthread
[In] identifier of the thread to which the message will be posted.
MSG
[In] specifies the type of message to be posted.
Wparam
[In] specifies additional message-specific information.
Lparam
[In] specifies additional message-specific information.
Return values

Nonzero indicates success. Zero indicates failure. To get extended Error
Information, call getlasterror.Getlasterror
Returns error_invalid_thread_id ifIdthreadIs not a valid thread
Identifier.

Remarks

The thread to which the message is posted retrieves the message by calling
The getmessage or peekmessage function.HwndMember
Of the returned MSG structure is null.

PostthreadmessageIt can be used for asynchronous communication between threads because it does not have to wait for the caller to return,
This may be the simplest method in thread communication.

However, pay attention to the following issues:
1. The postthreadmessage sometimes fails and the error 1444 (invalid thread identifier.) is reported .)
In fact, this is not necessarily because the thread does not exist, or it may be because the thread does not exist in message queue.
In fact, not every thread has a message queue. How can we make the thread have a message queue?
The answer is: Call the message-related functions at least once, such as getmessage and peekmessage.

2. If the memory dynamically allocated by post is assigned to another thread, pay attention to the correct release of memory.

3. postthreadmessage cannot post synchronous messages such as wm_copydate. Otherwise, an error is reported.

4. It is best not to use postthreadmessage post message to give a window, instead of postmessage.

The following is a strict example for my reference.

# Include < Windows. h >
# Include < Cstdio >
# Include < Process. h >

# DefineMy_msg wm_user+ 100
Const IntMax_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 , 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 ;
}

IntMain ()
{
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.