AsyncOperationWithEventHandler.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. namespace Wayne.Lib.AsyncManager
  3. {
  4. /// <summary>
  5. /// Async operation that contains a delegate that should be called upon completion.
  6. /// When the operation is completed, the Complete() method should be called, which results in that the result delegate
  7. /// will be invoked, and the operation will be removed from the Async Manager cache. Cancel can also be called, to remove
  8. /// the operation from the outstanding operation list without calling the delegate.
  9. /// </summary>
  10. /// <typeparam name="TOperationId">Type of the OperationId </typeparam>
  11. /// <typeparam name="TResultEventArgs"></typeparam>
  12. public class AsyncOperation<TOperationId, TResultEventArgs> : AsyncOperation<TOperationId> where TResultEventArgs : EventArgs
  13. {
  14. #region Fields
  15. private EventHandler<TResultEventArgs> resultDelegate;
  16. private Type resultEventArgsType;
  17. #endregion
  18. #region Construction
  19. /// <summary>
  20. /// Internal constructor.
  21. /// </summary>
  22. /// <param name="owner"></param>
  23. /// <param name="id"></param>
  24. /// <param name="userToken"></param>
  25. /// <param name="data"></param>
  26. /// <param name="resultDelegate"></param>
  27. /// <param name="abandonedTime"></param>
  28. internal AsyncOperation(object owner, TOperationId id, object userToken, object data, EventHandler<TResultEventArgs> resultDelegate, TimeSpan abandonedTime)
  29. : base(owner, id, userToken, data, abandonedTime)
  30. {
  31. this.resultDelegate = resultDelegate;
  32. resultEventArgsType = typeof(TResultEventArgs);
  33. }
  34. #endregion
  35. #region Properties
  36. internal override Type ResultEventArgsType
  37. {
  38. get { return resultEventArgsType; }
  39. }
  40. #endregion
  41. #region Methods
  42. /// <summary>
  43. /// Completes the operation, calls the result delegate with the specified event args.
  44. /// </summary>
  45. /// <param name="resultEventArgs"></param>
  46. public void Complete(TResultEventArgs resultEventArgs)
  47. {
  48. Complete();
  49. if (resultDelegate != null)
  50. resultDelegate(Owner, resultEventArgs);
  51. }
  52. #endregion
  53. #region Debug methods
  54. /// <summary>
  55. /// Presents the class as a string.
  56. /// </summary>
  57. /// <returns></returns>
  58. public virtual string ToString(string format, IFormatProvider provider)
  59. {
  60. string ownerName;
  61. IIdentifiableEntity identifiableOwner = Owner as IIdentifiableEntity;
  62. if (identifiableOwner != null)
  63. ownerName = IdentifiableEntity.ToString(identifiableOwner);
  64. else
  65. ownerName = this.Owner.ToString();
  66. return string.Format(System.Globalization.CultureInfo.InvariantCulture, "AsyncOperation<TOperatorId={0},TResultEventArgs={1}> Owner={2},Id={3},",
  67. typeof(TOperationId).FullName, typeof(TResultEventArgs).FullName, ownerName, Id);
  68. }
  69. /// <summary>
  70. /// Presents the class as a string using the specified culture-specific format information.
  71. /// </summary>
  72. /// <returns></returns>
  73. public virtual string ToString(IFormatProvider provider)
  74. {
  75. return ToString("", provider);
  76. }
  77. /// <summary>
  78. /// Presents the class as a string using a format string.
  79. /// </summary>
  80. /// <returns></returns>
  81. public virtual string ToString(string format)
  82. {
  83. return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
  84. }
  85. /// <summary>
  86. /// Presents the class as a string using a format string and the specified culture-specific format information.
  87. /// </summary>
  88. /// <returns></returns>
  89. public override string ToString()
  90. {
  91. return ToString("", System.Globalization.CultureInfo.InvariantCulture);
  92. }
  93. #endregion
  94. }
  95. }