AsyncOperation.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. namespace Wayne.Lib.AsyncManager
  3. {
  4. /// <summary>
  5. /// An Async Operation stores the necessary data for one asynchronous call. It has a
  6. /// type parameter that represents what type of identifier the operation has, i.e. the type of
  7. /// sequence number, token etc.
  8. /// </summary>
  9. public abstract class AsyncOperation<TOperationId>
  10. {
  11. #region Fields
  12. #endregion
  13. #region Events
  14. /// <summary>
  15. /// Internal event that is fired when the operation is completed. This will enable the AsyncManager to remove
  16. /// the operation from the list of outstanding operations.
  17. /// </summary>
  18. internal event EventHandler OnOperationCompleted;
  19. /// <summary>
  20. /// Event that let application react to internal cleanup of this operation.
  21. /// </summary>
  22. public event EventHandler OnOperationAbanoned;
  23. #endregion
  24. #region Construction
  25. /// <summary>
  26. /// Protected constructor of the AsyncOperation
  27. /// </summary>
  28. /// <param name="owner">The owner that will stand as sender when calling the delegate at completion.</param>
  29. /// <param name="id">Owner, that will stand as sender in the completion callback invocation.</param>
  30. /// <param name="userToken">User token to be supplied in the callback invocation.</param>
  31. /// <param name="data">User defined data.</param>
  32. /// <param name="maxTimespan"></param>
  33. protected internal AsyncOperation(object owner, TOperationId id, object userToken, object data, TimeSpan maxTimespan)
  34. {
  35. Timeout = new TimeoutInterval(maxTimespan);
  36. this.Owner = owner;
  37. this.Id = id;
  38. this.UserToken = userToken;
  39. this.Data = data;
  40. }
  41. #endregion
  42. #region Properties
  43. /// <summary>
  44. /// The owner of the operation. It will be set as the sender when calling the delegate at completion
  45. /// </summary>
  46. public object Owner { get; private set; }
  47. /// <summary>
  48. /// The identification for the operation.
  49. /// </summary>
  50. public TOperationId Id { get; private set; }
  51. /// <summary>
  52. /// The user token that identifies the operation.
  53. /// </summary>
  54. public object UserToken { get; private set; }
  55. /// <summary>
  56. /// Timeout of the operation, when it is considered to be abandoned.
  57. /// </summary>
  58. internal TimeoutInterval Timeout { get; set; }
  59. /// <summary>
  60. /// User-defined data property that can be used to store application data with the operation.
  61. /// </summary>
  62. public object Data { get; set; }
  63. internal abstract Type ResultEventArgsType
  64. {
  65. get;
  66. }
  67. #endregion
  68. #region Methods
  69. /// <summary>
  70. /// Internal method to be called by descendant classes when the operation is completed and should be removed from the outstanding operations.
  71. /// </summary>
  72. internal protected void Complete()
  73. {
  74. if (OnOperationCompleted != null)
  75. OnOperationCompleted(this, EventArgs.Empty);
  76. }
  77. /// <summary>
  78. /// Internal method to be called by descendant classes when the operation is cleaned out, invoking the OnOperationAbanoded event.
  79. /// </summary>
  80. internal protected void Abandoned()
  81. {
  82. OnOperationAbanoned.Fire(this, EventArgs.Empty);
  83. Cancel();
  84. }
  85. /// <summary>
  86. /// Removes the operation from the Async Manager lists, but does not call the completion delegate.
  87. /// </summary>
  88. public void Cancel()
  89. {
  90. Complete();
  91. }
  92. #endregion
  93. }
  94. }