Message Queue knowledge

Source: Internet
Author: User

In the MFC framework, there are two types of threads: one is the MFC thread that comes with the message queue, the other is the worker thread, and the working thread does not have the message queue. Therefore

 

To use a message queue for a worker thread, you only need to call peekmessage () in the worker thread.

 

1. The displayed call peekmessage ()

 

Peekmessage (& MSG, null, wm_user, wm_user, pm_noremove); // force to make the queue

 

2. postthreadmessage ()

 

Postthreadmessage (m_threadid, wm_close, 0, 0 );

 

When calling postthreadmessage (), the corresponding thread must have a message queue; otherwise, the message cannot be sent successfully.

 

3. sendmessage ():
Function: This function sends a specified message to one or more windows. This function calls the window program for the specified window until the window program finishes processing the message and returns it. The postmessage function returns a message immediately after it is sent to the Message Queue of a thread.

Function prototype: lresult sendmessage (hwnd, uint MSG, wparam, lparam iparam );

Parameters:

Hwnd: the handle of the window whose window program will receive messages. If this parameter is hwnd_broadcast, the message is sent to all top-level windows in the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows, however, messages are not sent to subwindows.

MSG: Specifies the message to be sent.

Wparam: Specify the additional message.

Iparam: Specify the attached message.

Return Value: the return value specifies the message processing result, depending on the sent message.

Note: applications that need to communicate with hwnd_broadcast should use the registerwindowmessage function to obtain a unique message for communications between applications.

If the specified window is created by the calling thread, the window program is called as a subroutine immediately. If the specified window is created by different threads, the system switches to the thread and calls the appropriate window program. Messages between threads are processed only when the thread executes the message retrieval code. The sending thread is blocked until the receiving thread finishes processing the message.

Windows CE: Windows CE does not support all messages supported by Windows desktop. Before using sendmesssge, check whether the sent message is supported.

Quick query: Windows NT: 3.1 and later versions: Windows: 95 and later versions; Windows CE: 1.0 and later versions; header file: winuser. h. Input Library: user32.lib; UNICODE: Unicode and ANSI in Windows NT.

Postmessage:
Function: This function puts a message (sent) to the Message Queue associated with the thread created in the specified window, and returns the message without waiting for the thread to process the message. Messages in a message queue are obtained by calling getmessage and peekmessage.

Function prototype: b00l postmessage (hwnd, uint MSG, wparam, lparam );

Parameters

Hwnd: the handle of the window in which the window program receives messages. Two values with specific meanings are recommended:

Hwnd. Broadcast: all top-level windows sent to the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows. Messages are not sent to subwindows.

Null: the operation of this function is the same as that of the postthreadmessage function that calls the dwthread parameter to set it to the current thread identifier.

MSG: Specifies the message to be sent.

Wparam: specify the specific information of the attached message.

Iparam: specify the specific information of the attached message.

Return Value: If the function call is successful, a non-zero value is returned. If the function call fails, the return value is zero. To obtain more error information, call the getlasterror function.

Note: applications that need to communicate in hwnd_broadcast mode should use the registerwindwosmessage function to obtain unique messages for inter-Application Communication.

If you send a message below the wm_user range to the asynchronous message function (postmessage. sendpolicymessage, sendmesssgecallback), the Message Parameter cannot contain a pointer. Otherwise, the Operation will fail. The function will receive a message before the thread processes the message, and the sender will be released before the memory is used.

 

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.