BOOL bend=FALSE;
UINT CThreDlg::ThreadFunction(LPVOID pParam)
{
while(!bend){
Beep(100,100);
        Sleep(1000);
}
return 0;
}CWinThread *pThread;
HWND hWnd;BOOL CThreDlg::OnInitDialog() 
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
hWnd = GetSafeHwnd();
    pThread = AfxBeginThread(ThreadFunction, hWnd);
    pThread->m_bAutoDelete = FALSE;
return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}void CThreDlg::OnCancel() 
{
// TODO: Add your control notification handler code here
bend=TRUE;//改变变量,线程结束
    WaitForSingleObject(pThread->m_hThread,INFINITE);//等待线程结束
    delete pThread;//删除线程
}编译不通过:error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'找半天找不出来,郁闷了...

解决方案 »

  1.   

    线程函数必须是静态的,如果要访问类的成员,可以把对象的this指针传给线程函数的void指针参数,到函数内部再使用指针转换恢复出本来的类型。
      

  2.   

    至于为什么必须是静态的,因为AfxBeginThread函数的原型声明中所声明的就不是类的成员函数,注意类的非静态成员函数和一般的函数在调用机制上就不一样(多一个隐含的this指针参数),所以,无法把一个类的成员函数赋给一个一般的函数指针。