AsyncOperation.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #endregion
  20. #region Construction
  21. /// <summary>
  22. /// Protected constructor of the AsyncOperation
  23. /// </summary>
  24. /// <param name="owner">The owner that will stand as sender when calling the delegate at completion.</param>
  25. /// <param name="id">Owner, that will stand as sender in the completion callback invocation.</param>
  26. /// <param name="userToken">User token to be supplied in the callback invocation.</param>
  27. /// <param name="data">User defined data.</param>
  28. internal protected AsyncOperation(object owner, TOperationId id, object userToken, object data):this(owner, id, userToken, data, ServiceContainerFactory.Create())
  29. {
  30. }
  31. /// <summary>
  32. /// Protected constructor of the AsyncOperation
  33. /// </summary>
  34. /// <param name="owner">The owner that will stand as sender when calling the delegate at completion.</param>
  35. /// <param name="id">Owner, that will stand as sender in the completion callback invocation.</param>
  36. /// <param name="userToken">User token to be supplied in the callback invocation.</param>
  37. /// <param name="data">User defined data.</param>
  38. /// <param name="serviceLocator"></param>
  39. internal protected AsyncOperation(object owner, TOperationId id, object userToken, object data, IServiceLocator serviceLocator)
  40. {
  41. ISystemTime systemTime = serviceLocator.GetServiceOrDefault<ISystemTime>(() => new WayneSystemTime());
  42. CreationDateTime = systemTime.Now;
  43. this.Owner = owner;
  44. this.Id = id;
  45. this.UserToken = userToken;
  46. this.Data = data;
  47. }
  48. #endregion
  49. #region Properties
  50. /// <summary>
  51. /// The owner of the operation. It will be set as the sender when calling the delegate at completion
  52. /// </summary>
  53. public object Owner { get; private set; }
  54. /// <summary>
  55. /// The identification for the operation.
  56. /// </summary>
  57. public TOperationId Id { get; private set; }
  58. /// <summary>
  59. /// The user token that identifies the operation.
  60. /// </summary>
  61. public object UserToken { get; private set; }
  62. /// <summary>
  63. /// Creation date time that enables a cleaning of the outstanding operation list.
  64. /// </summary>
  65. internal DateTime CreationDateTime { get; set; }
  66. /// <summary>
  67. /// User-defined data property that can be used to store application data with the operation.
  68. /// </summary>
  69. public object Data { get; set; }
  70. internal abstract Type ResultEventArgsType
  71. {
  72. get;
  73. }
  74. #endregion
  75. #region Methods
  76. /// <summary>
  77. /// Internal method to be called by descendant classes when the operation is completed and should be removed from the outstanding operations.
  78. /// </summary>
  79. internal protected void Complete()
  80. {
  81. if (OnOperationCompleted != null)
  82. OnOperationCompleted(this, EventArgs.Empty);
  83. }
  84. /// <summary>
  85. /// Removes the operation from the Async Manager lists, but does not call the completion delegate.
  86. /// </summary>
  87. public void Cancel()
  88. {
  89. Complete();
  90. }
  91. #endregion
  92. }
  93. }