AsyncResult.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #region License
  2. /* Copyright 2012 James F. Bellinger <http://www.zer7.com/software/hidsharp>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  13. #endregion
  14. using System;
  15. using System.Threading;
  16. namespace HidSharp
  17. {
  18. class AsyncResult<T> : IAsyncResult
  19. {
  20. volatile bool _isCompleted;
  21. ManualResetEvent _waitHandle;
  22. AsyncResult(AsyncCallback callback, object state)
  23. {
  24. AsyncCallback = callback; AsyncState = state;
  25. }
  26. void Complete()
  27. {
  28. lock (this)
  29. {
  30. if (_isCompleted) { return; }
  31. _isCompleted = true;
  32. if (_waitHandle != null) { _waitHandle.Set(); }
  33. }
  34. if (AsyncCallback != null) { AsyncCallback(this); }
  35. }
  36. internal delegate T OperationCallback();
  37. internal static IAsyncResult BeginOperation(OperationCallback operation,
  38. AsyncCallback callback, object state)
  39. {
  40. var ar = new AsyncResult<T>(callback, state);
  41. ThreadPool.QueueUserWorkItem(delegate (object self)
  42. {
  43. try { ar.Result = operation(); }
  44. catch (Exception e) { ar.Exception = e; }
  45. ar.Complete();
  46. }, ar);
  47. return ar;
  48. }
  49. internal T EndOperation()
  50. {
  51. while (true)
  52. {
  53. if (IsCompleted)
  54. {
  55. if (Exception != null) { throw Exception; }
  56. return Result;
  57. }
  58. AsyncWaitHandle.WaitOne();
  59. }
  60. }
  61. public AsyncCallback AsyncCallback
  62. {
  63. get;
  64. private set;
  65. }
  66. public object AsyncState
  67. {
  68. get;
  69. private set;
  70. }
  71. public WaitHandle AsyncWaitHandle
  72. {
  73. get
  74. {
  75. lock (this)
  76. {
  77. if (_waitHandle == null)
  78. {
  79. _waitHandle = new ManualResetEvent(_isCompleted);
  80. }
  81. }
  82. return _waitHandle;
  83. }
  84. }
  85. public bool CompletedSynchronously
  86. {
  87. get { return false; }
  88. }
  89. public bool IsCompleted
  90. {
  91. get { return _isCompleted; }
  92. }
  93. Exception Exception
  94. {
  95. get;
  96. set;
  97. }
  98. T Result
  99. {
  100. get;
  101. set;
  102. }
  103. }
  104. }