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;}
}
}
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;}
}
}
解决方案 »
- 单选按钮的选中问题
- 改不了CL istCtrl控件的背景色,高手指点.
- 求NDIS的两个例子:Passthru 和 Mux
- 我想请问一下有谁知道怎样计算出一个三维立体的不规则堆场的体积???要用三角剖分吗?
- 求救!!VC中一次运算32位数值怎样处理?溢出!!在线等待。
- 请问为什么是这样的结果?前面4行有typedef的,我看不懂
- 如何用代码方式为非模态对话框动态添加已经准备好的菜单资源!
- 大家来抢分呀! : 怎样 使用 "多媒体计时器"
- 为什么在屏幕保护时,杀不死错误弹出框?急
- 新手上路(谁先回答就给谁分)
- VS2008 sp1开发的程序默认字体模糊,如何修改
- 用string作为map的key性能高吗?
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
这个有什么作用啊,从消息队列里面移除,什么用?线程什么的....
要是断不下来 就把while改成if...
我如果要等待10个线程结束的话,怎么知道什么时候结束呢?这么弄呢?
if(dRet==WAIT_OBJECT_0+Threadcount)MsgWaitForMultipleObjects的返回值的范围:WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1)
你的第一个if判断不可能成功
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.怎么不可能成功
但是CPU运转的速度是惊人的,你调试相当于将CPU的速度拖慢了几百万倍,正常运行这些都没问题。
hotpos
大神,你说说我这个为什么线程那段if不执行?这是为什么?你能说说嘛?
{
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;
}
{
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(); //结束鼠标等待状态
}
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;
}
你运行下看看
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
直接替换成:PeekMessage(&msg,NULL,0,0,PM_REMOVE)