c++ - How to define CWinThread in MFC? -
i have set int
variable "editbox" control in c++/mfc. want change value in thread.
i define thread bellow :
cwinthread *pthread(); uint functionthread(cthdlg& d) { dword result = 0; int = 0; while (1) { if (i == 5000) = 0; d.m_text1 = i; i++; d.updatedata(false); } return result; } void cthdlg::onbnclickedok() { // todo: add control notification handler code here pthread = afxbeginthread(functionthread, thread_priority_normal); }
where's problem?
you should run code in debug mode , under debugger, see assertion.
the problem mfc allows access window thread created window. in case means main thread can access windows, worker thread can not. updatedata
accessing windows, not work in worker thread.
so need signal worker thread main thread new value available , shall displayed. signaling can post window message dialog window (postmessage
). sure not use sendmessage
because block until message received. might run dead lock if main thread waiting worker thread , worker thread waiting main thread in sendmessage
. when main thread receives message can update window control.
btw, code not valid. afxbeginthread
requires afx_threadproc
declared uint __cdecl mycontrollingfunction(lpvoid pparam);
. need change thread function to
uint __cdecl functionthread(lpvoid pparam) { cthdlg& d = *reinterpret_cast<cthdlg*>(pparam);
Comments
Post a Comment