AsyncOperationWithEventHandler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. internal AsyncOperation(object owner, TOperationId id, object userToken, object data, EventHandler<TResultEventArgs> resultDelegate)
  28. : this(owner, id, userToken, data, resultDelegate, ServiceContainerFactory.Create())
  29. {
  30. }
  31. /// <summary>
  32. /// Internal constructor.
  33. /// </summary>
  34. /// <param name="owner"></param>
  35. /// <param name="id"></param>
  36. /// <param name="userToken"></param>
  37. /// <param name="data"></param>
  38. /// <param name="resultDelegate"></param>
  39. /// <param name="serviceLocator"></param>
  40. internal AsyncOperation(object owner, TOperationId id, object userToken, object data, EventHandler<TResultEventArgs> resultDelegate, IServiceLocator serviceLocator)
  41. : base(owner, id, userToken, data, serviceLocator)
  42. {
  43. this.resultDelegate = resultDelegate;
  44. resultEventArgsType = typeof(TResultEventArgs);
  45. }
  46. #endregion
  47. #region Properties
  48. internal override Type ResultEventArgsType
  49. {
  50. get { return resultEventArgsType; }
  51. }
  52. #endregion
  53. #region Methods
  54. /// <summary>
  55. /// Completes the operation, calls the result delegate with the specified event args.
  56. /// </summary>
  57. /// <param name="resultEventArgs"></param>
  58. public void Complete(TResultEventArgs resultEventArgs)
  59. {
  60. Complete();
  61. if (resultDelegate != null)
  62. resultDelegate(Owner, resultEventArgs);
  63. }
  64. #endregion
  65. #region Debug methods
  66. /// <summary>
  67. /// Presents the class as a string.
  68. /// </summary>
  69. /// <returns></returns>
  70. public virtual string ToString(string format, IFormatProvider provider)
  71. {
  72. string ownerName;
  73. IIdentifiableEntity identifiableOwner = Owner as IIdentifiableEntity;
  74. if (identifiableOwner != null)
  75. ownerName = IdentifiableEntity.ToString(identifiableOwner);
  76. else
  77. ownerName = this.Owner.ToString();
  78. return string.Format(System.Globalization.CultureInfo.InvariantCulture, "AsyncOperation<TOperatorId={0},TResultEventArgs={1}> Owner={2},Id={3},",
  79. typeof(TOperationId).FullName, typeof(TResultEventArgs).FullName, ownerName, Id);
  80. }
  81. /// <summary>
  82. /// Presents the class as a string using the specified culture-specific format information.
  83. /// </summary>
  84. /// <returns></returns>
  85. public virtual string ToString(IFormatProvider provider)
  86. {
  87. return ToString("", provider);
  88. }
  89. /// <summary>
  90. /// Presents the class as a string using a format string.
  91. /// </summary>
  92. /// <returns></returns>
  93. public virtual string ToString(string format)
  94. {
  95. return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
  96. }
  97. /// <summary>
  98. /// Presents the class as a string using a format string and the specified culture-specific format information.
  99. /// </summary>
  100. /// <returns></returns>
  101. public override string ToString()
  102. {
  103. return ToString("", System.Globalization.CultureInfo.InvariantCulture);
  104. }
  105. #endregion
  106. }
  107. }