MsgWaitForMultipleObjects WaitForMultipleObjects

本文详细介绍了Windows API中的等待函数,包括WaitForSingleObject、WaitForMultipleObjects及MsgWaitForMultipleObjects的功能与用法。这些函数用于多线程环境中,能够帮助开发者有效管理线程间的同步与等待。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 函数的原形  
  DWORD   MsgWaitForMultipleObjects(  
      DWORD   nCount,                   //pHandle所指的handle数组中元素的个数  
      LPHANDLE   pHandle,           //指向一个由对象handle所组成的数组  
      BOOL   fWaitAll,                 //如果此值为TRUE,表示所有的HANDLE都被激发,此函数才返回  
                                                  //否则,此函数将在任何一个HANDLE激发时就返回  
      DWORD   dwMilliseconds,   //当该时间终了时,即使没有任何handles激发,此  
                                                  //函数也会返回。此值可为0,以便测试。也可以指定  
                                                  //为INFINITE,表示无穷等待  
      DWORD   dwWakeMask             //预观察的用户输入消息,可以是:  
  )                                               //QS_ALLINPUT     QS_KEY  
                                                  //QS_HOTKEY         QS_MOUSE  
                                                  //QS_INPUT           QS_MOUSEBUTTON  
                                                  //QS_MOUSEMOVE   QS_PAINT  
                                                  //QS_POSTMESSAGE       QS_SENDMESSAGE  
                                                  //QS_TIMER  
   
  这个函数是用来在GUI现成中等待对象被激发,是用来修改主消息循环的  
  这个函数非常像WaitForMultipleObjects(),但是他会在“对象被激发”  
  或“消息到达队列”时被唤醒而返回。MsgWaitForMultipleObject()函数  
  比WaitForMultipleObject()多接受一个参数,允许指定哪些消息是观察对象  
  MsgWaitForMultipleObject()的正确使用方式是改写主消息循环,使得激发状态  
  的HANDLE得以像消息一样被等待!  

 

 

WaitForSingleObject的用法

DWORD WaitForSingleObject(
  HANDLE hHandle,
  DWORD dwMilliseconds
);
参数hHandle是一个事件的句柄,第二个参数dwMilliseconds是时间间隔。如果时间是有信号状态返回WAIT_OBJECT_0,如果时间超过dwMilliseconds值但时间事件还是无信号状态则返回WAIT_TIMEOUT。
hHandle可以是下列对象的句柄:
    Change notification
Console input
Event
Job
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable timer

WaitForSingleObject函数用来检测hHandle事件的信号状态,当函数的执行时间超过 dwMilliseconds就返回,但如果参数dwMilliseconds为INFINITE时函数将直到相应时间事件变成有信号状态才返回,否则就 一直等待下去,直到WaitForSingleObject有返回直才执行后面的代码。在这里举个例子:
先创建一个全局Event对象g_event:
    CEvent g_event;
在程序中可以通过调用CEvent::SetEvent设置事件为有信号状态。
下面是一个线程函数MyThreadPro()
UINT CFlushDlg::MyThreadProc( LPVOID pParam )

{

     WaitForSingleObject(g_event,INFINITE);

     For(;;)

        {

         ………….

        }

     return 0;

}

在这个线程函数中只有设置g_event为有信号状态时才执行下面的for循环,因为g_event是全局变量,所以我们可以在别的线程中通过g_event. SetEvent控制这个线程。

 

还有一种用法就是我们可以通过WaitForSingleObject函数来间隔的执行一个线程函数的函数体
     UINT CFlushDlg::MyThreadProc( LPVOID pParam )

{

     while(WaitForSingleObject(g_event,MT_INTERVAL)!=WAIT_OBJECT_0)

     {

         ………………

     }

     return 0;

}

在这个线程函数中可以可以通过设置MT_INTERVAL来控制这个线程的函数体多久执行一次,当事件为无信号状态是函数体隔MT_INTERVAL执行一次,当设置事件为有信号状态时,线程就执行完毕了。
 
 
WaitForMultipleObjects
 
The WaitForMultipleObjects function returns when one of the following occurs:
 

Either any one or all of the specified objects are in the signaled state.
The time-out interval elapses.
 
To enter an alertable wait state, use the WaitForMultipleObjectsEx function.
DWORD WaitForMultipleObjects(
  DWORD nCount,             // number of handles in array
  CONST HANDLE *lpHandles,  // object-handle array
  BOOL bWaitAll,            // wait option
  DWORD dwMilliseconds      // time-out interval
);
 

 
Parameters
 
 
nCount
[in] Specifies the number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS.
lpHandles
[in] Pointer to an array of object handles. For a list of the object types whose handles can be specified, see the following Remarks section. The array can contain handles to objects of different types. It may not contain the multiple copies of the same handle.
If one of these handles is closed while the wait is still pending, the function's behavior is undefined.

Windows NT/2000/XP: The handles must have SYNCHRONIZE access. For more information, see Standard Access Rights.

Windows 95/98/Me: No handle may be a duplicate of another handle created using DuplicateHandle.
 
bWaitAll
[in] Specifies the wait type. If TRUE, the function returns when the state of all objects in the lpHandles array is signaled. If FALSE, the function returns when the state of any one of the objects is set to signaled. In the latter case, the return value indicates the object whose state caused the function to return.
dwMilliseconds
[in] Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the conditions specified by the bWaitAll parameter are not met. If dwMilliseconds is zero, the function tests the states of the specified objects and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.
 
Return Values
 

If the function succeeds, the return value indicates the event that caused the function to return. This value can be one of the following.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值