创建与对话框一起使用的线程

发布于 2024-11-14 21:28:06 字数 547 浏览 0 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三人与歌 2024-11-21 21:28:06

线程函数应该接收一个作为 pParam 传递给 AfxBeginThread 的参数,并且您可以将指针传递给您的类,并从线程函数调用任何类你想要的成员函数。我就是这么做的。

像这样的东西(伪代码):

CWinThread* thread = AfxBeginThread(
   MyClass::ThreadFunc,
   this);

MyClass::ThreadProc(LPARAM pParam)
{
    MyClass cls = dynamic_cast<MyClass*>(pParam);
    cls->RealThreadFunc();
};

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();
};
甜中书 2024-11-21 21:28:06

从辅助线程“接触”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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文