Troubleshooting of updatedata errors in threads

Source: Internet
Author: User

Http://hi.baidu.com/leonardhuang/blog/item/d5fa133f814777cc9e3d62ee.html

Recently, I wrote a program and needed to update window data in my own work thread. When updatadata (false) is used to implement the current situation, an error is always reported. I searched the internet to find that updatedata cannot be used in the working thread to update data in the main thread. It is best to use the main thread (interface thread) to execute corresponding operations. To execute interface thread operations in a subthread (working thread), it is best to send messages to the main thread. Because cwnd of MFC uses TLS, It is not thread-safe.

Solution: 1. Define a message # define wm_update_message (wm_user + 200) 2. Define message ing Begin_message_map (ccserverdlg, cdialog)
// {Afx_msg_map (ccserverdlg)
...
On_message (wm_update_message, onupdatemessage)
//} Afx_msg_map
End_message_map () 3. Define the Message Processing Function // Generated message map Functions
// {Afx_msg (ccserverdlg )...
Afx_msg lresult onupdatemessage (wparam, lparam );
//} Afx_msg
Declare_message_map () 4. To complete the function content, you only need to call updatedata () to obtain the lresult cxxxdlg: onupdatemessage (wparam, lparam)
{
Updatedata (false );
Return 0;
} 5. calls in the thread Uint cxxxdlg: clientthread (lpvoid lpparam)
{Cxxxdlg * PT = (ccserverdlg *) cwnd: fromhandle (ccserverdlg *) lpparam)-> m_hwnd); // get the handle Post messagept-> postmessage (wm_update_message, 0, 0); // OK-updatedate // message sended to "PT "...

}

6. Calls in the program // Cwinthread * pthread;
// Static uint clientthread (lpvoid lpparm ); Void ccserverdlg: onsstart ()
{... Pthread = afxbeginthread (clientthread, this );...}

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.