我写了一个线程函数,运行一次,线程正常运行:第一个switch(dwStatus)语句中执行case WAIT_OBJECT_0;      然后不退出主程序,再运行一次这个线程,则在第一个switch(dwStatus)语句中为WAIT_TIMEOUT.这是为什么?怎么控制WaitForMultipleObjects的返回值?我的线程函数如下:
static void _Thread_FuncPtr(LPVOID hCmpp)
{ HANDLE hEvent1=CreateEvent(NULL,false,false,"initscandlgok");//此时由主线程调用f(),见下
::WaitForSingleObject(hEvent1,INFINITE);
::ResetEvent(hEvent1);
Sleep(200);

HANDLE hEvents[2];

hEvents[0]=::CreateEvent(NULL,false,false,"eventReadyToExecute"); // Set when plugin list and prefs received
hEvents[1]=::CreateEvent(NULL,false,false,"eventStatusUpdate");  // Set when connection closed
dwStatus=WaitForMultipleObjects(2,hEvents,FALSE,45000);   // Wait 45 seconds

switch(dwStatus)
{
case WAIT_OBJECT_0:  // Ready
break;
case WAIT_OBJECT_0+1:  // Connection closed or STATUS message received
MessageBox("什么时候才进这里??????????");
goto exit_scan_thread;
case WAIT_TIMEOUT:
MessageBox("超时退出");
goto exit_scan_thread;
default:
break;
} //****************
……
//在这里是其它与事件无关的代码

hEvents[0]=::CreateEvent(NULL,false,false,"eventStatusUpdate");
hEvents[1]=::CreateEvent(NULL,false,false,"eventRemoveFinished");//set when removefinish is checked
while(1)
{
dwStatus=WaitForMultipleObjects(2,hEvents,FALSE,INFINITE);
if ((!CClientDlg::isConnected)||(CScanDlg::scanfinished))    // Scan finished
break;
switch(dwStatus)
{
case WAIT_OBJECT_0:
            MessageBox("evevntSatatusUpdate");
            break;
case (WAIT_OBJECT_0+1):
            // Remove all already finished hosts if needed
//对事件2的响应吗?
            break;
default:
            break;
}
}
exit_scan_thread : HANDLE h1 = OpenEvent(EVENT_ALL_ACCESS,FALSE,"eventReadyToExecute");
CloseHandle(h1);
HANDLE h2 = OpenEvent(EVENT_ALL_ACCESS,FALSE,"eventStatusUpdate");
CloseHandle(h1);
HANDLE h3 = OpenEvent(EVENT_ALL_ACCESS,FALSE,"eventRemoveFinished");
CloseHandle(h1); ::_endthread();

}void f()//此函数为了使程序能够往执行
{
HANDLE hEvent=CreateEvent(NULL,false,false,"initscandlgok");
::SetEvent(hEvent);
}

解决方案 »

  1.   

    没太细看: )
    不过_Thread_FuncPtr中
    hEvents[0]=::CreateEvent(NULL,false,false,"eventReadyToExecute"); // Set when plugin list and prefs received
    hEvents[1]=::CreateEvent(NULL,false,false,"eventStatusUpdate"); // Set when connection closed
    dwStatus=WaitForMultipleObjects(2,hEvents,FALSE,45000); // Wait 45 seconds线程结束后,CloseHandle了没有?
    如果没有,下次又创建?这会失败的,设断点看看
      

  2.   

    楼主给你一些提示,聪明人一点就通
     WaitForMultipleObject(DWORD dwCount
                            CONST HNADLE* phObjects
                            BOOL fWaitALL
                            DWORD dwMilliseconds)你把第三个参数设置的是FALSE,则多个event中有一个就到达就可以执行,返回值就是WAIT_OBJECT_0+通知到达的event在数据中的index.....
      

  3.   

    楼主给你一些提示,聪明人一点就通
     WaitForMultipleObject(DWORD dwCount
                            CONST HNADLE* phObjects
                            BOOL fWaitALL
                            DWORD dwMilliseconds)你把第三个参数设置的是FALSE,则多个event中有一个就到达就可以执行,返回值就是WAIT_OBJECT_0+通知到达的event在数据中的index.....
      

  4.   

    楼主给你一些提示,聪明人一点就通
     WaitForMultipleObject(DWORD dwCount
                            CONST HNADLE* phObjects
                            BOOL fWaitALL
                            DWORD dwMilliseconds)你把第三个参数设置的是FALSE,则多个event中有一个就到达就可以执行,返回值就是WAIT_OBJECT_0+通知到达的event在数据中的index.....