I need to make a thread using AfxBeginThread in a MFC application that uses some dialog boxes, but because the thread function is inside a class I have to make it static, and then I can't use any controls because they are not stati开发者_运维问答c, and even if I make them static I get some linker errors.
Can someone tell me how its the correct way to implement this ? Do i really need to declar the controls static ? Or is there any other way to do this ?
This are the errors (without static)
error C2228: left of '.AddString' must have class/struct/union
With static:
unresolved external symbol "public: static class CListBox CsearchDlg::m_musicList" (?m_musicList@CsearchDlg@@2VCListBox@@A)
The thread function is supposed to receive a parameter that you pass to AfxBeginThread
as pParam
, and you can pass there the pointer to your class, and from the thread function call whatever class member function you want. That's how I do it.
Something like this (pseudo code):
CWinThread* thread = AfxBeginThread(
MyClass::ThreadFunc,
this);
MyClass::ThreadProc(LPARAM pParam)
{
MyClass cls = dynamic_cast<MyClass*>(pParam);
cls->RealThreadFunc();
};
It's usually not a good idea to "touch" the UI from secondary threads. It's better to leave all the UI interaction to the main thread and just post messages from the secondary thread to the main one.
精彩评论