关于WINDOWS API WaitForMultipleObjects 问题(50分求解)(50分)

  • 主题发起人 主题发起人 foison
  • 开始时间 开始时间
F

foison

Unregistered / Unconfirmed
GUEST, unregistred user!
函数WaitForMultipleObjects可以同时监测多个同步对象,该函数的声明为:<br>DWORD WaitForMultipleObjects(DWORD nCount, CONST HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds );<br>参数nCount是句柄数组中句柄的数目。lpHandles代表一个句柄数组。bWaitAll说明了等待类型,如果为TRUE,那么函数在所有对象都有信号后才返回,如果为FALSE,则只要有一个对象变成有信号的,函数就返回。参数dwMilliseconds是以毫秒为单位的超时间隔,如果该参数为0,那么函数就测试同步对象的状态并立即返回,如果该参数为INFINITE,则超时间隔是无限的。<br>[red]问题:如果将dwMilliseconds设为INFINITE,怎样在没有监测到结果的情况下,中断WaitForMultipleObjects的监测?[/red]<br><br><br><br>问题:如果将dwMilliseconds设为INFINITE,怎样在没有监测到结果的情况下,中断WaitForMultipleObjects的监测?
 
在lpHandles的Event列表中加一个FStop事件。<br>当应用程序要结束WaitForMultipleObjects等待状态时,SetEvent(FStop);
 
能具体一点儿么
 
FStop事件怎样加呢<br>我有点儿愚笨请您给一段加事件的代码好么?
 
&nbsp;TMyThread = class(TThread)<br>&nbsp; private<br>&nbsp; &nbsp; FStopEvent: THandle;<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; &nbsp; procedure Stop;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create;<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp; end;<br><br>constructor TMyThread.Create;<br>begin<br>&nbsp; inherited Create(True);<br>&nbsp; FStopEvent := CreateEvent(nil, True, False, nil);<br>&nbsp; // execute thread<br>&nbsp; Resume;<br>end;<br><br>// destroy thread<br>destructor TMyThread.Destroy;<br>begin<br>&nbsp; Stop;<br>&nbsp; inherited Destroy;<br>end; &nbsp;<br><br>// stop thread<br>procedure TMyThread.Stop;<br>begin<br>&nbsp; SetEvent(FStopEvent); &nbsp;// 停止WaitForMultipleObjects<br>&nbsp; Sleep(0);<br>end;<br><br>// thread action<br>procedure TMyThread.Execute;<br>var<br>&nbsp; EventHandles: array[0..5] of THandle;<br>&nbsp; Signaled: DWORD;<br>begin<br>&nbsp; EventHandles[0] := FStopEvent; &nbsp;// 加停止事件<br>&nbsp; EventHandles[1] := ...;<br>&nbsp; EventHandles[2] := ...;<br>&nbsp; EventHandles[3] := ...;<br>&nbsp; EventHandles[4] := ...;<br>&nbsp; EventHandles[5] := ...;<br>&nbsp; repeat<br>&nbsp; &nbsp; Signaled := WaitForMultipleObjects(6, @EventHandles, False, INFINITE);<br>&nbsp; &nbsp; .....<br>&nbsp; until Signaled &lt;&gt; (WAIT_OBJECT_0 + 1);<br>&nbsp; CloseHandle(FStopEvent);<br>end;<br>
 
谢谢,我选看看!
 
再谢谢您!<br>请你先到<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=2450627<br>留个贴给你15分.<br>这个贴我还想留一留!:)(想试通了再给分)
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2530013
 
接受答案了.
 
后退
顶部