WorkItem.WorkItemResult.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Threading;
  3. namespace Amib.Threading.Internal
  4. {
  5. public partial class WorkItem
  6. {
  7. #region WorkItemResult class
  8. private class WorkItemResult : IWorkItemResult, IInternalWorkItemResult, IInternalWaitableResult
  9. {
  10. /// <summary>
  11. /// A back reference to the work item
  12. /// </summary>
  13. private readonly WorkItem _workItem;
  14. public WorkItemResult(WorkItem workItem)
  15. {
  16. _workItem = workItem;
  17. }
  18. internal WorkItem GetWorkItem()
  19. {
  20. return _workItem;
  21. }
  22. #region IWorkItemResult Members
  23. public bool IsCompleted
  24. {
  25. get
  26. {
  27. return _workItem.IsCompleted;
  28. }
  29. }
  30. public bool IsCanceled
  31. {
  32. get
  33. {
  34. return _workItem.IsCanceled;
  35. }
  36. }
  37. public object GetResult()
  38. {
  39. return _workItem.GetResult(Timeout.Infinite, true, null);
  40. }
  41. public object GetResult(int millisecondsTimeout, bool exitContext)
  42. {
  43. return _workItem.GetResult(millisecondsTimeout, exitContext, null);
  44. }
  45. public object GetResult(TimeSpan timeout, bool exitContext)
  46. {
  47. return _workItem.GetResult((int)timeout.TotalMilliseconds, exitContext, null);
  48. }
  49. public object GetResult(int millisecondsTimeout, bool exitContext, WaitHandle cancelWaitHandle)
  50. {
  51. return _workItem.GetResult(millisecondsTimeout, exitContext, cancelWaitHandle);
  52. }
  53. public object GetResult(TimeSpan timeout, bool exitContext, WaitHandle cancelWaitHandle)
  54. {
  55. return _workItem.GetResult((int)timeout.TotalMilliseconds, exitContext, cancelWaitHandle);
  56. }
  57. public object GetResult(out Exception e)
  58. {
  59. return _workItem.GetResult(Timeout.Infinite, true, null, out e);
  60. }
  61. public object GetResult(int millisecondsTimeout, bool exitContext, out Exception e)
  62. {
  63. return _workItem.GetResult(millisecondsTimeout, exitContext, null, out e);
  64. }
  65. public object GetResult(TimeSpan timeout, bool exitContext, out Exception e)
  66. {
  67. return _workItem.GetResult((int)timeout.TotalMilliseconds, exitContext, null, out e);
  68. }
  69. public object GetResult(int millisecondsTimeout, bool exitContext, WaitHandle cancelWaitHandle, out Exception e)
  70. {
  71. return _workItem.GetResult(millisecondsTimeout, exitContext, cancelWaitHandle, out e);
  72. }
  73. public object GetResult(TimeSpan timeout, bool exitContext, WaitHandle cancelWaitHandle, out Exception e)
  74. {
  75. return _workItem.GetResult((int)timeout.TotalMilliseconds, exitContext, cancelWaitHandle, out e);
  76. }
  77. public bool Cancel()
  78. {
  79. return Cancel(false);
  80. }
  81. public bool Cancel(bool abortExecution)
  82. {
  83. return _workItem.Cancel(abortExecution);
  84. }
  85. public object State
  86. {
  87. get
  88. {
  89. return _workItem.m_state;
  90. }
  91. }
  92. /// <summary>
  93. /// Return the result, same as GetResult()
  94. /// </summary>
  95. public object Result
  96. {
  97. get { return GetResult(); }
  98. }
  99. /// <summary>
  100. /// Returns the exception if occured otherwise returns null.
  101. /// This value is valid only after the work item completed,
  102. /// before that it is always null.
  103. /// </summary>
  104. public object Exception
  105. {
  106. get { return _workItem.m_exception; }
  107. }
  108. #endregion
  109. #region IInternalWorkItemResult Members
  110. public event WorkItemStateCallback OnWorkItemStarted
  111. {
  112. add
  113. {
  114. _workItem.OnWorkItemStarted += value;
  115. }
  116. remove
  117. {
  118. _workItem.OnWorkItemStarted -= value;
  119. }
  120. }
  121. public event WorkItemStateCallback OnWorkItemCompleted
  122. {
  123. add
  124. {
  125. _workItem.OnWorkItemCompleted += value;
  126. }
  127. remove
  128. {
  129. _workItem.OnWorkItemCompleted -= value;
  130. }
  131. }
  132. #endregion
  133. #region IInternalWorkItemResult Members
  134. public IWorkItemResult GetWorkItemResult()
  135. {
  136. return this;
  137. }
  138. public IWorkItemResult<TResult> GetWorkItemResultT<TResult>()
  139. {
  140. return new WorkItemResultTWrapper<TResult>(this);
  141. }
  142. #endregion
  143. }
  144. #endregion
  145. }
  146. }