DWORD dRet=0;
MSG msg;
  //int nExitThreadCount=0,totalThread=Threadcount; while(1)
{
dRet=::MsgWaitForMultipleObjects(Threadcount,hThreadp,FALSE,INFINITE,QS_ALLINPUT);
if(dRet==WAIT_OBJECT_0+Threadcount)
{
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if (dRet>=WAIT_OBJECT_0 && dRet<WAIT_OBJECT_0+Threadcount)
{
nExitThreadCount++;
if(Threadcount!=0){
int Index=dRet-WAIT_OBJECT_0;
hThreadp[Index]=hThreadp[Threadcount-1];
hThreadp[Threadcount-1]=NULL;
Threadcount--;}
else
{break;}

}

}

解决方案 »

  1.   

    上面这个会出问题,一直在while(peekmessage(...)){}里面,不跳到第二个if里面去呢?导致一直在大循环里面循环,一直无法跳出循环!大神来解决啊!还有就是给我解释一下while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    这个有什么作用啊,从消息队列里面移除,什么用?线程什么的....
      

  2.   

    处理完消息队列里面堆积的消息,这地方不能单步调试。直接在第二个if上面下断点。
    要是断不下来 就把while改成if...
      

  3.   

    不知道什么调不到第二个if每次都是等于第一个if的条件,然后你把大while改成if的话,
    我如果要等待10个线程结束的话,怎么知道什么时候结束呢?这么弄呢?
      

  4.   

    每次都是第一个if是因为唤醒事件QS_ALLINPUT被触发了, QS_ALLINPUT 表示当前线程有任何消息就触发
      

  5.   

    dRet=::MsgWaitForMultipleObjects(Threadcount,hThreadp,FALSE,INFINITE,QS_ALLINPUT);
    if(dRet==WAIT_OBJECT_0+Threadcount)MsgWaitForMultipleObjects的返回值的范围:WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1)
    你的第一个if判断不可能成功
      

  6.   

    WAIT_OBJECT_0 + nCountNew input of the type specified in the dwWakeMask parameter is available in the thread's input queue. Functions such as PeekMessage, GetMessage, and WaitMessage  messages in the queue as old messages. Therefore, after you call one of these functions, a subsequent call to MsgWaitForMultipleObjects will not return until new input of the specified type arrives. 
    This value is also returned upon the occurrence of a system event that requires the thread's action, such as foreground activation. Therefore, MsgWaitForMultipleObjects can return even though no appropriate input is available and even if dwWakeMask is set to 0. If this occurs, call GetMessage or PeekMessage to process the system event before trying the call to MsgWaitForMultipleObjects again.怎么不可能成功
     
      

  7.   

    不是的吧,第一个是有消息来的时候也会触发好吧!我调试了,都是在第一个if里面!不跳到第二个if里面去根本!
      

  8.   

    那我怎么让线程结束啊?都一直在第一个if里面!怎么跳到第二个if呢?
      

  9.   

    我习惯暴力等待,就是第三个参数填True,最后个参数填0 ..这样会在所有线程退出后 MsgWaitForMultipleObjects 才会返回。你这个不用管它,你在第二个if里面下断点,等有线程退出的时候自然就能断下来
      

  10.   

    QS_ALLINPUT会使有任何的消息到达消息队列都会结束 MsgWaitForMultipleObjects  的等待,包括键盘、鼠标、以及调试时引起的窗口重绘等。尤其是调试时引起的窗口重绘。
    但是CPU运转的速度是惊人的,你调试相当于将CPU的速度拖慢了几百万倍,正常运行这些都没问题。
      

  11.   

    恩,是这样的,我像你那样子说的,我从新新建了个程序,用你说的,暴力等待,还是不行直接就是返回值有错!这个函数是不是有BUG啊?
      

  12.   


    hotpos
    大神,你说说我这个为什么线程那段if不执行?这是为什么?你能说说嘛?
      

  13.   

    UINT CCallWebService::CallWebTheread(LPVOID pParam)
    {
    UserNameMsg *msgdlg=(UserNameMsg *)pParam;
    CCallWebService *dlg=msgdlg->s_dlg; CTime mtim;
    CString strtime,speedT;
    CString slang = msgdlg->strLang;
    int pid = msgdlg->pidnumb;
    CString sCountry = msgdlg->strCountry;
    DWORD oldtime,sptime; if (SUCCEEDED(CoInitialize(NULL)))
    {
    CSecurity getToken;
    Security::GenericMessage logmsg;
    ParentChildConsoleReturn parentMsg;
    HRESULT hr; BSTR name=(msgdlg->str_name).AllocSysString();
    BSTR paswd=(msgdlg->str_pwd).AllocSysString(); BSTR stoken;
    bool exitmak=false;
    while(TRUE)//循环 调用webservice
    {
    if (dlg->isexit)
    {
    dlg->InsertInfo(_T(""),_T(""),_T(""),false,_T(""));
    break;
    }
    mtim = CTime::GetCurrentTime(); 
    strtime=mtim.Format("%Y-%m-%d  %H:%M:%S");//访问开始时间
    oldtime=GetTickCount();
    hr=getToken.eMailLogin(name,paswd,&parentMsg);//得到 token
    sptime=(GetTickCount()-oldtime);// 计算时间差
    speedT.Format(_T("%d ms"),sptime);
    if (SUCCEEDED(hr))
    {
    //OutputDebugString(_T("eMailLogin success!"));
    stoken=parentMsg.sToken;
    dlg->InsertInfo(strtime,_T("eMailLogin"),speedT,false,dlg->path1);
    if (!(dlg->TokenTraverse(stoken,pid,slang,sCountry,paswd)))//返回值为 false 也就是 退出的标志
    {
    exitmak=true;
    }
    if (!(dlg->TraverseNoToken(pid,slang,sCountry)))
    {
    exitmak=true;
    }
    }
    else//登陆失败 现在继续访问不需要token的,或者是改成登陆失败继续登陆?
    {
    dlg->InsertInfo(strtime,_T("eMailLogin"),speedT,true,dlg->path1);
    if (!(dlg->TraverseNoToken(pid,slang,sCountry)))//返回值为 false 也就是 退出的标志
    {
    exitmak=true;
    }
    }
    mtim = CTime::GetCurrentTime(); 
    strtime=mtim.Format("%Y-%m-%d  %H:%M:%S");//访问开始时间
    oldtime=GetTickCount();
    hr=getToken.logout(&logmsg);
    sptime=(GetTickCount()-oldtime);// 计算时间差
    speedT.Format(_T("%d ms"),sptime);
    if (SUCCEEDED(hr))
    dlg->InsertInfo(strtime,_T("logout"),speedT,false,dlg->path1);
    else
    dlg->InsertInfo(strtime,_T("logout"),speedT,true,dlg->path1);
    //OutputDebugString(_T("logout 00000000000000"));
    if (exitmak)
    {
    break;
    }
    Sleep(500);
    }
    SysFreeString(name);
    SysFreeString(paswd);
    //CoUninitialize();//会导致程序崩溃,具体原因不清楚,感觉手动清除com组件有问题?
    }
    else
    {
    AfxMessageBox(_T("CoInitialize fail!"));
    }
    delete msgdlg;
    return 0;
    }
      

  14.   

    void CCallWebService::OnBnClickedButton1()
    {
    BeginWaitCursor(); //鼠标变成等待
    CString strtext;

    ((CButton*)GetDlgItem(IDC_BUTTON1))->GetWindowText(strtext);
    if (_T("CallWebService")==strtext)
    {
    CString strXmlElem;
    isexit=false;
    //m_buttonCtrl.LoadBitmaps(IDB_BITMAP2);
    CString strunm;
    m_combCrtl.GetWindowText(strunm);
    Threadcount=atoi(strunm);
    if (Threadcount<=0)//线程数量 少于0 就不处理
    {
    MessageBox(_T("Please first create XML account file or Check the XML file!"),_T("CallWebService"),MB_OK|MB_ICONEXCLAMATION);
    return;
    }
    m_buttonCtrl.EnableWindow(FALSE);
    m_combCrtl.EnableWindow(FALSE);//禁用 下拉列表框
    if(m_listmsg.GetItemCount()>0)//列表不为空就 删除所有的
    m_listmsg.DeleteAllItems();
    if(NULL!=hThreadp)
    {
    delete []hThreadp;
    hThreadp=NULL;
    }
    hThreadp = new HANDLE[Threadcount+1];
    for (int i=0;i<Threadcount;i++)//先读取xml中的配置文件
    {
    UserNameMsg *namemsg=new UserNameMsg;//要在这里 new一个区域,否则传入到线程中会出问题,删除的时候由线程完成
    namemsg->s_dlg=this;
    strXmlElem.Format(_T("%d"),(i+1));
    strXmlElem=_T("AccountInfo")+strXmlElem;//读取对应的xml 项下的信息
    //OutputDebugString(strXmlElem);
    if (m_XmlFile.FindElem(strXmlElem))
    {
    m_XmlFile.IntoElem();//进入下一个目录
    if (m_XmlFile.FindElem(_T("UserName")))
    namemsg->str_name=m_XmlFile.GetData();
    else
    namemsg->str_name=_T("[email protected]");//默认就会调用这个账号

    if (m_XmlFile.FindElem(_T("Password")))
    namemsg->str_pwd=m_XmlFile.GetData();
    else
    namemsg->str_pwd=_T("e10adc3949ba59abbe56e057f20f883e");
    m_XmlFile.OutOfElem();//退回去
    m_XmlFile.ResetMainPos();//要 置回去,否则多次访问的时候会出现问题
    }
    else
    {
    namemsg->str_name=_T("[email protected]");//默认账号
    namemsg->str_pwd=_T("e10adc3949ba59abbe56e057f20f883e");
    }
    namemsg->pidnumb=mpidnu;
    namemsg->strLang=mlanguage;
    namemsg->strCountry=mcountry;
    CWinThread* pWinT=AfxBeginThread(&CallWebTheread,LPVOID(namemsg));
    hThreadp[i] = pWinT->m_hThread;
    }
    ((CButton*)GetDlgItem(IDC_BUTTON1))->SetWindowText(_T("StopCall"));
    m_buttonCtrl.EnableWindow(TRUE); //开启这个按钮
    //isrunTd=true;
    }
    else
    {
    isexit=true;
    m_buttonCtrl.EnableWindow(FALSE);
    m_ButtonSave.EnableWindow(FALSE);
    m_WaitFlag=true; //鼠标变成等待

    //WaitForMultipleObjects(Threadcount,hThreadp,TRUE,INFINITE);// 这个会使主界面卡死

    //等待线程结束,因为要在主界面上面添加记录,所以要刷新界面WaitForMultipleObjects会阻塞,
    // 改用MsgWaitForMultipleObjects实现
    DWORD dRet=0;
    MSG msg;
      //int nExitThreadCount=0,totalThread=Threadcount; while(1)
    {
    dRet=::MsgWaitForMultipleObjects(Threadcount,hThreadp,FALSE,INFINITE,QS_ALLINPUT);
    if(dRet==WAIT_OBJECT_0+Threadcount)
    {
    while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    if (dRet>=WAIT_OBJECT_0 && dRet<WAIT_OBJECT_0+Threadcount)
    {
    nExitThreadCount++;
    if(Threadcount!=0){
    int Index=dRet-WAIT_OBJECT_0;
    hThreadp[Index]=hThreadp[Threadcount-1];
    hThreadp[Threadcount-1]=NULL;
    Threadcount--;}
    else
    {break;}

    }

    }
    //bool isforce=false;
    //for(int j=0;j< Threadcount ;j++)
    //{
    //while (1)
    //{
    //dRet=::MsgWaitForMultipleObjects(1,hThreadp +j,FALSE,INFINITE,QS_ALLINPUT);//每次等待一个结束
    //if (dRet == WAIT_OBJECT_0 + 1)
    //{//在这里有可能卡死,具体原因还不清楚
    // while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    // {
    // TranslateMessage(&msg);
    // DispatchMessage(&msg);
    // }
    // if(nExitThreadCount==Threadcount)//获得的退出数量和线程数量一致的时候 强制退出
    // {
    // isforce=true;
    // break;
    // }
    //}
    //else 
    //{
    // break;
    //}
    //if ((dRet >= WAIT_OBJECT_0) && (dRet < WAIT_OBJECT_0 + Threadcount))
    //{
    // nExitThreadCount++;
    // if (nExitThreadCount < totalThread)//一个线程退出了
    // {
    // TRACE("一个线程退出了\n");
    // int nIndex=dRet-WAIT_OBJECT_0;//退出的线程 数组中的
    // for (int nj=nIndex;nj<Threadcount;nj++)
    // {
    // hThreadp[nj]=hThreadp[nj+1];
    // }
    // hThreadp[Threadcount-1]=NULL;
    // Threadcount--;
    // }
    // else
    // {
    // break;
    // }
    //}
    //else
    //{
    // DWORD dErrCode=GetLastError();
    // break;
    //}
    //}
    /*if (isforce)
    {
    nExitThreadCount=0;
    break;
    }*/
    //OutputDebugString(_T("MsgWaitForMultipleObjects111111111111"));
    //}
    if (hThreadp!=NULL)
    {
    delete []hThreadp;
    hThreadp=NULL;
    }
    ((CButton*)GetDlgItem(IDC_BUTTON1))->SetWindowText(_T("CallWebService"));
    m_combCrtl.EnableWindow(TRUE);
    m_buttonCtrl.EnableWindow(TRUE); //开启这个按钮
    m_ButtonSave.EnableWindow(TRUE);
    m_WaitFlag=false; //结束鼠标等待状态
    }
    EndWaitCursor(); //结束鼠标等待状态
    }
      

  15.   


    DWORD WINAPI ThreadFun(LPVOID threadParam)
    {
    int n = (int)threadParam;
    for (int i=0; i<n; i++)
    {
    Sleep(1000);
    } printf("thread %d quit.\n", n);
    return 0;
    }void WaitForThreadQuit(HANDLE hTheads[], int threadCount)
    {
    MSG msg; while(threadCount > 0)
    {
    DWORD dRet = MsgWaitForMultipleObjects(threadCount,hTheads,FALSE,INFINITE,QS_ALLINPUT); if (dRet == (WAIT_OBJECT_0 + threadCount))
    {
    while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    else if ((dRet>=WAIT_OBJECT_0) && (dRet<WAIT_OBJECT_0 + threadCount))
    {
    int quitIndex = dRet - WAIT_OBJECT_0; printf("hTheads[%d] quit.\n", quitIndex); HANDLE hThread = hTheads[quitIndex];
    hTheads[quitIndex] = hTheads[--threadCount];
    hTheads[threadCount] = hThread;
    }
    }
    }int _tmain(int argc, _TCHAR* argv[])
    {
    const int threadCount = 6;
    HANDLE hThreads[threadCount] = {0}; for (int i=0; i<threadCount; i++)
    {
    hThreads[i] = CreateThread(NULL, 0, ThreadFun, (LPVOID)(i+1), 0, NULL);
    } WaitForThreadQuit(hThreads, threadCount);
    printf("all thread quit.\n"); return 0;
    }
    你运行下看看
      

  16.   

    线程卡住了?那我看看吧,但是..我也不知道怎么说了,我同事用了一种方法又能实现,就是在吧那个什么MSG什么的函数结构改了下就可以了!谢谢你啊!
      

  17.   

         while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
    直接替换成:PeekMessage(&msg,NULL,0,0,PM_REMOVE)
      

  18.   

    http://blog.csdn.net/silvervi/article/details/5874212