WorkItem.WorkItemResult.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /// <summary>
  95. /// Return the result, same as GetResult()
  96. /// </summary>
  97. public object Result
  98. {
  99. get { return GetResult(); }
  100. }
  101. /// <summary>
  102. /// Returns the exception if occured otherwise returns null.
  103. /// This value is valid only after the work item completed,
  104. /// before that it is always null.
  105. /// </summary>
  106. public object Exception
  107. {
  108. get { return _workItem._exception; }
  109. }
  110. #endregion
  111. #region IInternalWorkItemResult Members
  112. public event WorkItemStateCallback OnWorkItemStarted
  113. {
  114. add
  115. {
  116. _workItem.OnWorkItemStarted += value;
  117. }
  118. remove
  119. {
  120. _workItem.OnWorkItemStarted -= value;
  121. }
  122. }
  123. public event WorkItemStateCallback OnWorkItemCompleted
  124. {
  125. add
  126. {
  127. _workItem.OnWorkItemCompleted += value;
  128. }
  129. remove
  130. {
  131. _workItem.OnWorkItemCompleted -= value;
  132. }
  133. }
  134. #endregion
  135. #region IInternalWorkItemResult Members
  136. public IWorkItemResult GetWorkItemResult()
  137. {
  138. return this;
  139. }
  140. public IWorkItemResult<TResult> GetWorkItemResultT<TResult>()
  141. {
  142. return new WorkItemResultTWrapper<TResult>(this);
  143. }
  144. #endregion
  145. }
  146. #endregion
  147. }
  148. }