创建与对话框一起使用的线程
我需要使用 AfxBeginThread 在使用一些对话框的MFC应用程序中,但是因为线程函数位于类内部,所以我必须将其设置为静态,然后我无法使用任何控件,因为它们不是静态的,即使我使它们是静态的我收到一些链接器错误。
有人可以告诉我这是如何实现这个的正确方法吗?我真的需要将控件声明为静态吗?或者还有其他方法可以做到这一点吗?
这是错误(没有静态)
error C2228: left of '.AddString' must have class/struct/union
使用静态:
unresolved external symbol "public: static class CListBox CsearchDlg::m_musicList" (?m_musicList@CsearchDlg@@2VCListBox@@A)
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 static, 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
线程函数应该接收一个作为
pParam
传递给AfxBeginThread
的参数,并且您可以将指针传递给您的类,并从线程函数调用任何类你想要的成员函数。我就是这么做的。像这样的东西(伪代码):
The thread function is supposed to receive a parameter that you pass to
AfxBeginThread
aspParam
, 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):
从辅助线程“接触”UI 通常不是一个好主意。最好将所有 UI 交互留给主线程,而只将消息从辅助线程发布到主线程。
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.