多线程死机了?
我程序大概如下:
UINT CDlgInfo::ScanThread( LPVOID pParam)
{
CDlgInfo *pDlg=(CDlgInfo *)pParam;
    
pDlg->m_bPowerFlag=FALSE;
do
{
//等待事件
pDlg->WaitEventSend(); 
pDlg->SendSnmp();
//事件复位
SetEvent(pDlg->hEvent);
Sleep(1000);
        } 
while(pDlg->m_bPowerFlag);

    
return 0;
}void CDlgInfo::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
switch (nIDEvent)
{
    
     case 11:
KillTimer(11);

if(pScanThread==NULL)
{
pScanThread=AfxBeginThread(ScanThread,this);
                pScanThread->m_bAutoDelete=FALSE;
}
else if(WaitForSingleObject(pScanThread->m_hThread,0)==WAIT_OBJECT_0)
         { 

    if(pScanThread!=NULL)
         {
     delete pScanThread;//
     pScanThread=NULL;
       
         }

         pScanThread=AfxBeginThread(ScanThread,this);
pScanThread->m_bAutoDelete=FALSE;

         }
SetTimer(11,1000,NULL);
break;
}
}
程序的执行过程是这样的,现在有5个CDlgInfo类,当这个类生成的时候会自动开启一个线程,然后这个类会每隔
1s,检查一下,当前线程是否在执行,如果在执行的话,就什么都不做,否则重新开启一个线程,以此循环下去,
现在的问题就是:当运行一段时间后,程序就死机了,并且每次死机的时候都是当某个线程推出的时候,在debug窗口下
可以看到The thread 0x844 has exited with code 0 (0x0).
请问这是什么原因啊,我该怎样解决啊?

解决方案 »

  1.   

    是啊,程序的cpu占用率一直为0了
      

  2.   

    现在把程序改成这样:
    UINT CDlgInfo::ScanThread( LPVOID pParam)
    {
    CDlgInfo *pDlg=(CDlgInfo *)pParam;
        

        
    return 0;
    }
    如果我现在有5个CDlgInfo在运行的话,它还是会死机的。
      

  3.   

    写错了,程序是改成这样
    UINT CDlgInfo::ScanThread( LPVOID pParam)
    {
    CDlgInfo *pDlg=(CDlgInfo *)pParam;
        
    Sleep(1000);
        
    return 0;
    }
      

  4.   

    在工作线程中访问CWnd*????这本身就要不得,因为MFC类不是线程安全的。
      

  5.   

    最好不要用类成员来做线程,改为:看看
    UINT ScanThread( LPVOID pParam)
    {
    CDlgInfo *pDlg=(CDlgInfo *)pParam;
        
    pDlg->m_bPowerFlag=FALSE;
    do
    {
    //等待事件
    pDlg->WaitEventSend(); 
    pDlg->SendSnmp();
    //事件复位
    SetEvent(pDlg->hEvent);
    Sleep(1000);
            } 
    while(pDlg->m_bPowerFlag);

        
    return 0;
    }