我想让一个工作线程一段时间什么都不做,等待其他消息的唤醒。
不能用SLEEP,这样线程就任何消息都不接收了。
在我想的是这时线程应该只做RECIEVEMESSAGE和PEEKMEESSAGE这样的工作。请问各位,这样的函数该怎么写?

解决方案 »

  1.   

    临时设置一个时钟,SetTimer (hwnd, TimerID, 1000, NULL) ;
    在WM_TIMER事件中再继续程序就可以了
      

  2.   

    TO LIGHTWELL
    WAITFORSINGLEOBJECT 也是阻塞本线程的其他消息的。
      

  3.   

     MSG msg;
         PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
         BOOL bRet;
    DWORD dwtick = GetTickCount();
    int count =0;
    while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)

    if (bRet == -1)
    {
    // handle the error and possibly exit
    }
    else
    {
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    }
    if(GetTickCount()-dwtick>1000)
    break;
    }
      

  4.   

    用MsgWaitForMultipleObjects (),既能接受到WINDOWS消息 ,又能受到内核对象信号,
    记住它不会使线程挂起,
     int MessageLoop ( 
        HANDLE* lphObjects,  // handles that need to be waited on 
        int     cObjects     // number of handles to wait on 
      )

        // The message loop lasts until we get a WM_QUIT message,
        // upon which we shall return from the function.
        while (TRUE)
        {
            // block-local variable 
            DWORD result ; 
            MSG msg ;         // Read all of the messages in this next loop, 
            // removing each message as we read it.
            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
            { 
                // If it's a quit message, we're out of here.
                if (msg.message == WM_QUIT)  
                    return 1; 
                // Otherwise, dispatch the message.
                DispatchMessage(&msg); 
            } // End of PeekMessage while loop.        // Wait for any message sent or posted to this queue 
            // or for one of the passed handles be set to signaled.
            result = MsgWaitForMultipleObjects(cObjects, lphObjects, 
                     FALSE, INFINITE, QS_ALLINPUT);         // The result tells us the type of event we have.
            if (result == (WAIT_OBJECT_0 + cObjects))
            {
                // New messages have arrived. 
                // Continue to the top of the always while loop to 
                // dispatch them and resume waiting.
                continue;
            } 
            else 
            { 
                // One of the handles became signaled. 
                DoStuff (result - WAIT_OBJECT_0) ; 
            } // End of else clause.
        } // End of the always while loop. 
    } // End of function.
      

  5.   

    没必要那么麻烦,用SetTimer就可以了。