现派生
class CcomThread : public CWinThread
CcomThread * p_myThread;//定义指针对象p_myThread=(CcomThread*)AfxBeginThread(RUNTIME_CLASS(CcomThread),THREAD_PRIORITY_NORMAL,0,0); //开线程
现在想关掉
int nCount = 0; 
DWORD dwStatus;
if(p_myThread!=NULL)
{
VERIFY(::GetExitCodeThread(p_myThread->m_hThread,&dwStatus));
if(dwStatus==STILL_ACTIVE)
{
nCount++; 
m_bDone = TRUE;
}如果仍为运行状态,则终止
else{
    delete p_myThread;
p_myThread=NULL;
}}//如果已经终止,则删除该线程对象 }
if (nCount == 0) { // 线程已终止,则关闭程序 
CDialog::OnClose();// 在SDI和MDI程序中应改为void CMainFrame::OnClose()函数 

else // 否则,发送WM_CLOSE消息 
PostMessage(WM_CLOSE, 0, 0); 请编译error C2248: 'CcomThread::~CcomThread' : cannot access protected member declared in class 'CcomThread'
这个怎么处理?

解决方案 »

  1.   

    把向导自动生成的 ~CcomThread(); 从 protected 改为 public 此外:一般情况没必要对 AfxBeginThread 返回的 CWinThread 指针调用 delete ,因为新创建的 WinThread 会在线程终止后自动释放。但是这会让 pThread->m_hThread 这样的访问有可能出错,安全的方法是这样:pThread=(CcomThread*)AfxBeginThread(RUNTIME_CLASS(CcomThread), THREAD_PRIORITY_NORMAL, CREATE_SUSPENDED, 0);if (pThread)
    {
        pThread->m_bAutoDelete = FALSE;
        pThread->ResumeThread();
        ...
        delete pThread;
    }
      

  2.   

    自动生成的析构函数::~CcomThread' 是protected的,需要改成public
      

  3.   

    谢谢你的指点,不过现在
    运行库采用多线程
    :\program files\microsoft visual studio\vc98\mfc\include\afxver_.h(130) : fatal error C1189: #error :  Please use the /MD switch for _AFXDLL builds
    Error executing cl.exe.
    运行库采用多线程dll
    SMSTestDlg.obj : error LNK2005: "int  m_bDone" (?m_bDone@@3HA) already defined in SMSTest.obj
    LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other libs; use /NODEFAULTLIB:library
    Debug/SMSTest.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.
    m_bDone 只是一个全局布尔变量,根本没有定义int m_bDone,请指点!
      

  4.   

    1。强行终止线程用
    BOOL TerminateThread(
      HANDLE hThread,
      DWORD dwExitCode
    );然后WaitForSingleObject一下,再delete,不能直接delete。2。全局m_bDone所在的头文件被重复包含了。
    在别的地方要用的时候先extern int m_bDone定义一下。
      

  5.   

    去搜索CMultiThread,这个MSJ的古董线程类可以从外部kill的