一个MsgWaitForMultipleObjects函数的问题!解决五体投地的感谢!

longgang521125 2012-02-22 03:46:34
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;}

}

}
...全文
932 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
钱塘民工 2012-10-08
  • 打赏
  • 举报
回复
http://blog.csdn.net/silvervi/article/details/5874212
钱塘民工 2012-10-08
  • 打赏
  • 举报
回复
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
直接替换成:PeekMessage(&msg,NULL,0,0,PM_REMOVE)
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
线程卡住了?那我看看吧,但是..我也不知道怎么说了,我同事用了一种方法又能实现,就是在吧那个什么MSG什么的函数结构改了下就可以了!谢谢你啊!
hotpos 2012-02-22
  • 打赏
  • 举报
回复
你线程卡住了吧, 估计
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 hotpos 的回复:]
C/C++ code

DWORD WINAPI ThreadFun(LPVOID threadParam)
{
int n = (int)threadParam;
for (int i=0; i<n; i++)
{
Sleep(1000);
}

printf("thread %d quit.\n", n);
……
[/Quote]
你这个能运行额,我那个为什么呢?
hotpos 2012-02-22
  • 打赏
  • 举报
回复

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;
}


你运行下看看
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
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("nanjun@test.com");//默认就会调用这个账号

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("nanjun@test.com");//默认账号
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(); //结束鼠标等待状态
}
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
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;
}
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 hotpos 的回复:]
不会啊。 代码我看看
[/Quote]
所有的?有蛮多啊!
hotpos 2012-02-22
  • 打赏
  • 举报
回复
不会啊。 代码我看看
longgang521125 2012-02-22
  • 打赏
  • 举报
回复

hotpos
大神,你说说我这个为什么线程那段if不执行?这是为什么?你能说说嘛?
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hotpos 的回复:]
QS_ALLINPUT会使有任何的消息到达消息队列都会结束 MsgWaitForMultipleObjects 的等待,包括键盘、鼠标、以及调试时引起的窗口重绘等。

尤其是调试时引起的窗口重绘。
但是CPU运转的速度是惊人的,你调试相当于将CPU的速度拖慢了几百万倍,正常运行这些都没问题。
[/Quote]
恩,是这样的,我像你那样子说的,我从新新建了个程序,用你说的,暴力等待,还是不行直接就是返回值有错!这个函数是不是有BUG啊?
hotpos 2012-02-22
  • 打赏
  • 举报
回复
QS_ALLINPUT会使有任何的消息到达消息队列都会结束 MsgWaitForMultipleObjects 的等待,包括键盘、鼠标、以及调试时引起的窗口重绘等。

尤其是调试时引起的窗口重绘。
但是CPU运转的速度是惊人的,你调试相当于将CPU的速度拖慢了几百万倍,正常运行这些都没问题。
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hotpos 的回复:]
我习惯暴力等待,就是第三个参数填True,最后个参数填0 ..

这样会在所有线程退出后 MsgWaitForMultipleObjects 才会返回。

你这个不用管它,你在第二个if里面下断点,等有线程退出的时候自然就能断下来
[/Quote]
这样的话,不太好啊,我就不明白了,为什么他一直在第一个if里面!难道工作线程一直不结束?
hotpos 2012-02-22
  • 打赏
  • 举报
回复
我习惯暴力等待,就是第三个参数填True,最后个参数填0 ..

这样会在所有线程退出后 MsgWaitForMultipleObjects 才会返回。

你这个不用管它,你在第二个if里面下断点,等有线程退出的时候自然就能断下来
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hotpos 的回复:]
每次都是第一个if是因为唤醒事件QS_ALLINPUT被触发了, QS_ALLINPUT 表示当前线程有任何消息就触发
[/Quote]
那我怎么让线程结束啊?都一直在第一个if里面!怎么跳到第二个if呢?
longgang521125 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 visualeleven 的回复:]
dRet=::MsgWaitForMultipleObjects(Threadcount,hThreadp,FALSE,INFINITE,QS_ALLINPUT);
if(dRet==WAIT_OBJECT_0+Threadcount)

MsgWaitForMultipleObjects的返回值的范围:WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1)……
[/Quote]
不是的吧,第一个是有消息来的时候也会触发好吧!我调试了,都是在第一个if里面!不跳到第二个if里面去根本!
hotpos 2012-02-22
  • 打赏
  • 举报
回复
WAIT_OBJECT_0 + nCount

New input of the type specified in the dwWakeMask parameter is available in the thread's input queue. Functions such as PeekMessage, GetMessage, and WaitMessage mark 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.

怎么不可能成功
Eleven 2012-02-22
  • 打赏
  • 举报
回复
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判断不可能成功
hotpos 2012-02-22
  • 打赏
  • 举报
回复
每次都是第一个if是因为唤醒事件QS_ALLINPUT被触发了, QS_ALLINPUT 表示当前线程有任何消息就触发
加载更多回复(5)
API之网络函数1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同一个网络资源的连接 WNetCancelConnection 结束一个网络连接 WNetCancelConnection2 结束一个网络连接 WNetCloseEnum 结束一次枚举操作 WNetConnectionDialog 启动一个标准对话框,以便建立同网络资源的连接 WNetDisconnectDialog 启动一个标准对话框,以便断开同网络资源的连接 WNetEnumResource 枚举网络资源 WNetGetConnection 获取本地或已连接的一个资源的网络名称 WNetGetLastError 获取网络错误的扩展错误信息 WNetGetUniversalName 获取网络中一个文件的远程名称以及/或者UNC(统一命名规范)名称 WNetGetUser 获取一个网络资源用以连接的名字 WNetOpenEnum 启动对网络资源进行枚举的过程 2. API之消息函数 BroadcastSystemMessage 将一条系统消息广播给系统中所有的顶级窗口 GetMessagePos 取得消息队列中上一条消息处理完毕时的鼠标指针屏幕位置 GetMessageTime 取得消息队列中上一条消息处理完毕时的时间 PostMessage 将一条消息投递到指定窗口的消息队列 PostThreadMessage 将一条消息投递给应用程序 RegisterWindowMessage 获取分配给一个字串标识符的消息编号 ReplyMessage 答复一个消息 SendMessage 调用一个窗口的窗口函数,将一条消息发给那个窗口 SendMessageCallback 将一条消息发给窗口 SendMessageTimeout 向窗口发送一条消息 SendNotifyMessage 向窗口发送一条消息 3. API之文件处理函数 CloseHandle 关闭一个内核对象。其中包括文件、文件映射、进程、线程、安全和同步对象等 CompareFileTime 对比两个文件的时间 CopyFile 复制文件 CreateDirectory 创建一个新目录 CreateFile 打开和创建文件、管道、邮槽、通信服务、设备以及控制台 CreateFileMapping 创建一个新的文件映射对象 DeleteFile 删除指定文件 DeviceIoControl 对设备执行指定的操作 DosDateTimeToFileTime 将DOS日期和时间值转换成一个 win32 FILETIME 值 FileTimeToDosDateTime 将一个 win32 FILETIME 值转换成DOS日期和时间值 FileTimeToLocalFileTime 将一个FILETIME结构转换成本地时间 FileTimeToSystemTime 根据一个FILETIME结构的内容,装载一个SYSTEMTIME结构 FindClose 关闭由FindFirstFile函数创建的一个搜索句柄 FindFirstFile 根据文件名查找文件 FindNextFile 根据调用FindFirstFile函数时指定的一个文件名查找下一个文件 FlushFileBuffers 针对指定的文件句柄,刷新内部文件缓冲区 FlushViewOfFile 将写入文件映射缓冲区的所有数据都刷新到磁盘 GetBinaryType 判断文件是否可以执行 GetCompressedFileSize 判断一个压缩文件在磁盘上实际占据的字节数 GetCurrentDirectory 在一个缓冲区中装载当前目录 GetDiskFreeSpace 获取与一个磁盘的组织有关的信息,以及了解剩余空间的容量 GetDiskFreeSpaceEx 获取与一个磁盘的组织以及剩余空间容量有关的信息 GetDriveType 判断一个磁盘驱动器的类型 GetExpandedName 取得一个压缩文件的全名 GetFileAttributes 判断指定文件的属性 GetFileInformationByHandle 这个函数提供了获取文件信息的一种机制 GetFileSize 判断文件长度 GetFileTime 取得指定文件的时间信息 GetFileType 在给出文件句柄的前提下,判断文件类型 GetFileVersionInfo 从支持版本标记的一个模块里获取文件版本信息

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧