RegisteredWaitHandle.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // System.Threading.RegisteredWaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Copyright (C) 2004, 2005 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Runtime.InteropServices;
  33. namespace System.Threading
  34. {
  35. [ComVisible (true)]
  36. public sealed class RegisteredWaitHandle
  37. : MarshalByRefObject
  38. {
  39. WaitHandle _waitObject;
  40. WaitOrTimerCallback _callback;
  41. object _state;
  42. WaitHandle _finalEvent;
  43. ManualResetEvent _cancelEvent;
  44. TimeSpan _timeout;
  45. int _callsInProcess;
  46. bool _executeOnlyOnce;
  47. bool _unregistered;
  48. internal RegisteredWaitHandle (WaitHandle waitObject, WaitOrTimerCallback callback, object state, TimeSpan timeout, bool executeOnlyOnce)
  49. {
  50. _waitObject = waitObject;
  51. _callback = callback;
  52. _state = state;
  53. _timeout = timeout;
  54. _executeOnlyOnce = executeOnlyOnce;
  55. _finalEvent = null;
  56. _cancelEvent = new ManualResetEvent (false);
  57. _callsInProcess = 0;
  58. _unregistered = false;
  59. }
  60. internal void Wait (object state)
  61. {
  62. bool release = false;
  63. try {
  64. _waitObject.SafeWaitHandle.DangerousAddRef (ref release);
  65. try {
  66. WaitHandle[] waits = new WaitHandle[] {_waitObject, _cancelEvent};
  67. do {
  68. int signal = WaitHandle.WaitAny (waits, _timeout, false);
  69. if (!_unregistered) {
  70. lock (this) {
  71. _callsInProcess++;
  72. }
  73. ThreadPool.QueueUserWorkItem (new WaitCallback (DoCallBack), (signal == WaitHandle.WaitTimeout));
  74. }
  75. } while (!_unregistered && !_executeOnlyOnce);
  76. } catch {
  77. }
  78. lock (this) {
  79. _unregistered = true;
  80. if (_callsInProcess == 0 && _finalEvent != null)
  81. #if NETCORE
  82. throw new NotImplementedException ();
  83. #else
  84. NativeEventCalls.SetEvent (_finalEvent.SafeWaitHandle);
  85. #endif
  86. }
  87. } catch (ObjectDisposedException) {
  88. // Can happen if we called Unregister before we had time to execute Wait
  89. if (release)
  90. throw;
  91. } finally {
  92. if (release)
  93. _waitObject.SafeWaitHandle.DangerousRelease ();
  94. }
  95. }
  96. private void DoCallBack (object timedOut)
  97. {
  98. try {
  99. if (_callback != null)
  100. _callback (_state, (bool)timedOut);
  101. } finally {
  102. lock (this)
  103. {
  104. _callsInProcess--;
  105. if (_unregistered && _callsInProcess == 0 && _finalEvent != null)
  106. #if NETCORE
  107. throw new NotImplementedException ();
  108. #else
  109. NativeEventCalls.SetEvent (_finalEvent.SafeWaitHandle);
  110. #endif
  111. }
  112. }
  113. }
  114. [ComVisible (true)]
  115. public bool Unregister(WaitHandle waitObject)
  116. {
  117. lock (this)
  118. {
  119. if (_unregistered)
  120. return false;
  121. _finalEvent = waitObject;
  122. _unregistered = true;
  123. _cancelEvent.Set();
  124. return true;
  125. }
  126. }
  127. }
  128. }