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 );...}