2个回答
展开全部
我只是举个例子
class CMyAppDlg : public CDialog {
...
void CallThreadFunc(int a, int b);
static UINT ThreadFuncWrapper(LPVOID* p);
struct ThreadParam {
CMyAppDlg* pThisl;
int a;
int b;
}; //根据线程函数的参数而定
void ThreadFunc(int a, int b); //线程函数,自己写
...
};
void CMyAppDlg::CallThreadFunc(int a, int b) {
ThreadParam* tp;
*(LPVOID*)&tp = malloc(sizeof(ThreadParam));
tp.pThis = this;
tp.a = a;
tp.b = b;
CWinThread *t = AfxBeginThread(&CMyAppDlg::ThreadFuncWrapper, tp, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
t->m_bAutoDelete = TRUE;
t->ResumeThread();
}
UINT CMyAppDlg::ThreadFuncWrapper(LPVOID* p) {
ThreadParam param = *(ThreadParam*)p;
free(p);
param.pThis->ThreadFunc(param.a, param.b);
return 0;
}
调用的地方:
CallThreadFunc(1, 2);
像这样。
代码大概是这个意思,没测试编译。如果哪里有报错自己改一下应该可以……?
class CMyAppDlg : public CDialog {
...
void CallThreadFunc(int a, int b);
static UINT ThreadFuncWrapper(LPVOID* p);
struct ThreadParam {
CMyAppDlg* pThisl;
int a;
int b;
}; //根据线程函数的参数而定
void ThreadFunc(int a, int b); //线程函数,自己写
...
};
void CMyAppDlg::CallThreadFunc(int a, int b) {
ThreadParam* tp;
*(LPVOID*)&tp = malloc(sizeof(ThreadParam));
tp.pThis = this;
tp.a = a;
tp.b = b;
CWinThread *t = AfxBeginThread(&CMyAppDlg::ThreadFuncWrapper, tp, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
t->m_bAutoDelete = TRUE;
t->ResumeThread();
}
UINT CMyAppDlg::ThreadFuncWrapper(LPVOID* p) {
ThreadParam param = *(ThreadParam*)p;
free(p);
param.pThis->ThreadFunc(param.a, param.b);
return 0;
}
调用的地方:
CallThreadFunc(1, 2);
像这样。
代码大概是这个意思,没测试编译。如果哪里有报错自己改一下应该可以……?
更多追问追答
追问
那么我在线程函数中 声明了其他类的指针 指向了其它类的函数 这个能实现吗
追答
保证对象是存活的就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询