其中一段代码如下:
int num=0;
while (num<4)
{
DWORD dwResult=::WaitForSingleObject(m_myEvent,INFINITE);
//DWORD dwResult = MsgWaitForMultipleObjects( 1,&m_myEvent, FALSE, INFINITE, QS_ALLEVENTS );
switch (dwResult):
{
case:WAIT_OBJECT_0 :
{
break;
num=num+1;
}
CString str;
str.Format("%d",num);
AfxMessageBox(str);
}利用WaitForSingleObject得到num=4并且弹出对话框,说明正确得到四次m_myEvent事件,但是替换为MsgWaitForMultipleObjects却得不到num=4,而一直处于循环中,这是为什么?请高手块帮忙,多谢
int num=0;
while (num<4)
{
DWORD dwResult=::WaitForSingleObject(m_myEvent,INFINITE);
//DWORD dwResult = MsgWaitForMultipleObjects( 1,&m_myEvent, FALSE, INFINITE, QS_ALLEVENTS );
switch (dwResult):
{
case:WAIT_OBJECT_0 :
{
break;
num=num+1;
}
CString str;
str.Format("%d",num);
AfxMessageBox(str);
}利用WaitForSingleObject得到num=4并且弹出对话框,说明正确得到四次m_myEvent事件,但是替换为MsgWaitForMultipleObjects却得不到num=4,而一直处于循环中,这是为什么?请高手块帮忙,多谢
是你说的原因造成的,应该怎样解决呢?
HANDLE handleArray[2];
handleArray[0] = process;
DWORD SignaledObject(0xFF);
MSG msg;
char buff[20];
while(SignaledObject != WAIT_OBJECT_0)
{
SignaledObject = MsgWaitForMultipleObjects(1, handleArray, false,
30000, QS_P);
switch(SignaledObject)
{
case WAIT_OBJECT_0:
MessageBox("0 Signaled", "MsgWaitForMultipleObjects");
break; case WAIT_OBJECT_0+1:
// MessageBox("1 Signaled", "MsgWaitForMultipleObjects", MB_OK);
PeekMessage(&msg, theApp.m_pMainWnd->m_hWnd, WM_PAINT, WM_PAINT,
PM_REMOVE);
DispatchMessage(&msg);
break; default:
MessageBox(itoa(SignaledObject, buff, sizeof buff), "Signaled");
break; }
}MessageBox("Done", "MsgWaitForMultipleObjects", MB_OK);
CMyDialog::OnPlay()
{
bDone = FALSE;
while( !bDone )
{
dwResult = MsgWaitForMultipleObjects( 1, &g_hNotificationEvent,
FALSE, INFINITE, QS_ALLEVENTS );
switch( dwResult )
{
case WAIT_OBJECT_0 + 0:
{;}
break;case WAIT_OBJECT_0 + 1:
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( !IsDialogMessage( hDlg, &msg ) )
{
TranslateMessage( &msg );
} else if( msg.message == WM_COMMAND )
{
if(LOWORD(msg.wParam)==IDC_STOP)
bDone = TRUE;
}
}
break;
}
}
}//点击IDC_STOP按钮使上面的等待停止。
CMyDialog::OnStop()
{
}
可是点完play后就不能点其他的按钮了,难道必须开另一个线程?
谢谢!
你别在OnPlay里处理消息啊。你把消息peek出来,又没有dispatch把它们发给相应的窗口,当然没有反应了。
谢谢,能不能给详细点说说。
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( !IsDialogMessage( hDlg, &msg ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if( msg.message == WM_COMMAND )
{
if(LOWORD(msg.wParam)==IDC_STOP)
bDone = TRUE;
}
}
只有几个按钮。不应该直接放到按钮函数里去吧,
把应该开一个线程吧。
threadFuction()
{
//代码
}
其实如果你要peekmessage循环的话
完全没有必要用msgwaitformultiobject
一运行界面就僵住了,什么钮都点不了,
因此
if( msg.message == WM_COMMAND )
{
if(LOWORD(msg.wParam)==IDC_STOP)
bDone = TRUE;
}IDC_STOP的message就传不进去,一直循环着。我的本意是以为MsgWaitForMultipleObjects不会使界面僵住,但是事实是这样的,怎么办?