但是现在主线程由于WaitForMultipleObjects函数应该被阻塞

解决方案 »

  1.   

    AfxBeginThread返回CWinThread指针而不是句柄。调用WaitForMultipleObjects失败所以立即返回
      

  2.   

    既然i==1进入了死循环,那么ok不应该弹出啊
      

  3.   

    to wang_xiao_jian
    谢谢各位兄弟,我马上试一试,等我回来加分
      

  4.   

    wang_xiao_jian 是对的,你那样WaitForMultiObject返回WAIT_ERROR,错误代码为6(句柄无效)
    这样就可以了:
    ULONG _stdcall MyProc(LPVOID pParam)
    {
      int i=(int)pParam;
      while (i==1){Sleep(2);};
      CString temp;
      temp.Format("%d",i);
      
      MessageBox(0,temp,"",MB_OK);
      return 1;
    }void CAsynThreadDlg::OnTest() 
    {
    // TODO: Add your control notification handler code here
      HANDLE test[4];
      for (int i=0;i<4;i++)
        test[i]=CreateThread(NULL,0,MyProc,(LPVOID)i,0,NULL);
      Sleep(1000);
      SetLastError(0);
      DWORD WaitRes=WaitForMultipleObjects(4,test,true,INFINITE);
      DWORD Err=GetLastError();
      CString s;
      s.Format("%d",WaitRes);
      AfxMessageBox(s); 
      
    }
    偶不要分
      

  5.   

    我将程序改成下面的样子出现无响应的错误
    UINT MyProc(LPVOID pParam)
    {
      int i=(int)pParam;
      while (i==1);
      CString temp;
      temp.Format("%d",i);
      AfxMessageBox(temp);
      return 1;
    }
    void CTest::Main()
    {
      HANDLE test[4];
      for (int i=0;i<4;i++)
        test[i]=(AfxBeginThread(MyProc,(LPVOID)i))->m_hThread;
      WaitForMultipleObjects(4,test,true,INFINITE);
      AfxMessageBox("ok"); 
    }
      

  6.   

    1.把AfxmessageBox改成win32的messageBox,
    afxmessagebox要使用程序主窗口的句柄和消息流程,这时程序主线程已经进入WaitForMultipleObject的wait状态,没有响应,所以无法弹出。
    win32的messagebox就没有这个问题
    2.while(i==1);是线程控制一个很不好的代码风格,造成一个很“忙”的死循环,
    改成这样:while(i==1){Sleep(1);} 以交出部分线程时间
      

  7.   

    to panda_w
    虽然问题解决了,还是要谢谢你,可惜分都光了,下次再给你吧。