QQ网名大全

新建类吗,那如果我要单独把一个函数放入新线程中运

我只是举个例子

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);
像这样。
佚名
2024-11-16 09:55:46
最佳回答
类似问题(10)