1 using System
.Runtime
.InteropServices
;
3 namespace System
.Threading
5 public sealed class RegisteredWaitHandle
: MarshalByRefObject
7 WaitHandle _waitObject
;
8 WaitOrTimerCallback _callback
;
10 WaitHandle _finalEvent
;
11 ManualResetEvent _cancelEvent
;
14 bool _executeOnlyOnce
;
17 internal RegisteredWaitHandle (WaitHandle waitObject
, WaitOrTimerCallback callback
, object state
, TimeSpan timeout
, bool executeOnlyOnce
)
19 _waitObject
= waitObject
;
23 _executeOnlyOnce
= executeOnlyOnce
;
24 _cancelEvent
= new ManualResetEvent (false);
27 internal void Wait (object? state
)
31 _waitObject
.SafeWaitHandle
.DangerousAddRef (ref release
);
33 WaitHandle
[] waits
= new WaitHandle
[] {_waitObject, _cancelEvent}
;
35 int signal
= WaitHandle
.WaitAny (waits
, _timeout
, false);
40 ThreadPool
.QueueUserWorkItem (new WaitCallback (DoCallBack
), (signal
== WaitHandle
.WaitTimeout
));
42 } while (!_unregistered
&& !_executeOnlyOnce
);
48 if (_callsInProcess
== 0 && _finalEvent
!= null)
49 throw new NotImplementedException ();
51 } catch (ObjectDisposedException
) {
52 // Can happen if we called Unregister before we had time to execute Wait
57 _waitObject
.SafeWaitHandle
.DangerousRelease ();
61 void DoCallBack (object? timedOut
)
64 if (_callback
!= null)
65 _callback (_state
, (bool)timedOut
!);
69 if (_unregistered
&& _callsInProcess
== 0 && _finalEvent
!= null)
70 EventWaitHandle
.Set (_finalEvent
.SafeWaitHandle
);
75 public bool Unregister(WaitHandle waitObject
)
81 _finalEvent
= waitObject
;