123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- namespace Wayne.Lib.AsyncManager
- {
- /// <summary>
- /// An Async Operation stores the necessary data for one asynchronous call. It has a
- /// type parameter that represents what type of identifier the operation has, i.e. the type of
- /// sequence number, token etc.
- /// </summary>
- public abstract class AsyncOperation<TOperationId>
- {
- #region Fields
- #endregion
- #region Events
- /// <summary>
- /// Internal event that is fired when the operation is completed. This will enable the AsyncManager to remove
- /// the operation from the list of outstanding operations.
- /// </summary>
- internal event EventHandler OnOperationCompleted;
- /// <summary>
- /// Event that let application react to internal cleanup of this operation.
- /// </summary>
- public event EventHandler OnOperationAbanoned;
- #endregion
- #region Construction
- /// <summary>
- /// Protected constructor of the AsyncOperation
- /// </summary>
- /// <param name="owner">The owner that will stand as sender when calling the delegate at completion.</param>
- /// <param name="id">Owner, that will stand as sender in the completion callback invocation.</param>
- /// <param name="userToken">User token to be supplied in the callback invocation.</param>
- /// <param name="data">User defined data.</param>
- /// <param name="maxTimespan"></param>
- protected internal AsyncOperation(object owner, TOperationId id, object userToken, object data, TimeSpan maxTimespan)
- {
- Timeout = new TimeoutInterval(maxTimespan);
- this.Owner = owner;
- this.Id = id;
- this.UserToken = userToken;
- this.Data = data;
- }
- #endregion
- #region Properties
- /// <summary>
- /// The owner of the operation. It will be set as the sender when calling the delegate at completion
- /// </summary>
- public object Owner { get; private set; }
- /// <summary>
- /// The identification for the operation.
- /// </summary>
- public TOperationId Id { get; private set; }
- /// <summary>
- /// The user token that identifies the operation.
- /// </summary>
- public object UserToken { get; private set; }
- /// <summary>
- /// Timeout of the operation, when it is considered to be abandoned.
- /// </summary>
- internal TimeoutInterval Timeout { get; set; }
- /// <summary>
- /// User-defined data property that can be used to store application data with the operation.
- /// </summary>
- public object Data { get; set; }
- internal abstract Type ResultEventArgsType
- {
- get;
- }
- #endregion
- #region Methods
- /// <summary>
- /// Internal method to be called by descendant classes when the operation is completed and should be removed from the outstanding operations.
- /// </summary>
- internal protected void Complete()
- {
- if (OnOperationCompleted != null)
- OnOperationCompleted(this, EventArgs.Empty);
- }
- /// <summary>
- /// Internal method to be called by descendant classes when the operation is cleaned out, invoking the OnOperationAbanoded event.
- /// </summary>
- internal protected void Abandoned()
- {
- OnOperationAbanoned.Fire(this, EventArgs.Empty);
- Cancel();
- }
- /// <summary>
- /// Removes the operation from the Async Manager lists, but does not call the completion delegate.
- /// </summary>
- public void Cancel()
- {
- Complete();
- }
- #endregion
- }
- }
|