using System;
namespace Wayne.Lib.AsyncManager
{
///
/// 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.
///
public abstract class AsyncOperation
{
#region Fields
#endregion
#region Events
///
/// 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.
///
internal event EventHandler OnOperationCompleted;
///
/// Event that let application react to internal cleanup of this operation.
///
public event EventHandler OnOperationAbanoned;
#endregion
#region Construction
///
/// Protected constructor of the AsyncOperation
///
/// The owner that will stand as sender when calling the delegate at completion.
/// Owner, that will stand as sender in the completion callback invocation.
/// User token to be supplied in the callback invocation.
/// User defined data.
///
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
///
/// The owner of the operation. It will be set as the sender when calling the delegate at completion
///
public object Owner { get; private set; }
///
/// The identification for the operation.
///
public TOperationId Id { get; private set; }
///
/// The user token that identifies the operation.
///
public object UserToken { get; private set; }
///
/// Timeout of the operation, when it is considered to be abandoned.
///
internal TimeoutInterval Timeout { get; set; }
///
/// User-defined data property that can be used to store application data with the operation.
///
public object Data { get; set; }
internal abstract Type ResultEventArgsType
{
get;
}
#endregion
#region Methods
///
/// Internal method to be called by descendant classes when the operation is completed and should be removed from the outstanding operations.
///
internal protected void Complete()
{
if (OnOperationCompleted != null)
OnOperationCompleted(this, EventArgs.Empty);
}
///
/// Internal method to be called by descendant classes when the operation is cleaned out, invoking the OnOperationAbanoded event.
///
internal protected void Abandoned()
{
OnOperationAbanoned.Fire(this, EventArgs.Empty);
Cancel();
}
///
/// Removes the operation from the Async Manager lists, but does not call the completion delegate.
///
public void Cancel()
{
Complete();
}
#endregion
}
}