WorkItem.WorkItemResult.cs 5.4 KB

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