Several methods of controlling interface control in multi-threading of "turn" VC

Source: Internet
Author: User

Original URL: https://software.intel.com/zh-cn/blogs/2010/11/30/vc-3

In order to ensure that the user experience of the interface is often to put data processing into sub-threads, and then update the results to the main interface, there are usually several methods.

1. When starting a thread, it is undoubtedly the simplest method to pass a pointer to a control-associated variable to a thread function, but it is extremely easy to create an access exception because the controls in VC6 are not thread-safe.

2. Just a little bit more advanced method, the handle of the control to the thread function, sometimes it is not good to use in the sub-thread through SendNotifyMessage or PostMessage and other operations. This approach is thread-safe, but for many unlisted controls you do not know what message to send, such as the vast majority of ActiveX controls, such as MSFlexGrid, DBGRID, whose message IDs are unknown, and the second method is useless.

3. This approach I feel is the most versatile method, and the logic of the code is also the clearest. is in the window class for the operation of the thread to customize the message, start the thread directly to the window handle to the thread function, the thread to control the interface directly to the window to send a message. The window class adds a message map to the custom message, in which the specific control operation is performed.

Custom message:

VC Custom Message Method VC article 2010-07-11 16:02:54 read 2785 Comments 0 font size: Big Small
First step: Define the message.

#define Wm_my_message (WM_USER+100)

Step two: Implement the message handler function. The function uses the Wpram and lparam parameters and returns Lpesult

LRESULT dlg::onmymsg (WPARAM WPARAM, LPARAM LPARAM)
{

return 0;

}

Step three: Describe the message handler function in the afx_msg block of the class header file.

Virtual BOOL OnInitDialog ();
afx_msg void OnSysCommand (UINT NID, LPARAM LPARAM);
afx_msg void OnPaint ();
afx_msg hcursor Onquerydragicon ();
afx_msg LRESULT onmymsg (WPARAM, LPARAM); Copy Here

Declare_message_map ()

Fourth step: In the message block of the user class, use the ON_MESSAGE macro directive to map the message to the message handler function.

Begin_message_map (c auto-fill dlg, CDialog)
On_wm_syscommand ()
On_wm_paint ()
On_wm_querydragicon ()
On_message (Wm_my_message, onmymsg)//Copy Here

}}afx_msg_map
End_message_map ()

Note: The return value of the message handler function you added must be lresult, otherwise the "On_message conversion type is invalid" message appears!!!

Several methods of controlling interface control in multi-threading of "turn" VC

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.