using System;
namespace Wayne.Lib.AsyncManager
{
///
/// Async operation that contains a delegate that should be called upon completion.
/// When the operation is completed, the Complete() method should be called, which results in that the result delegate
/// will be invoked, and the operation will be removed from the Async Manager cache. Cancel can also be called, to remove
/// the operation from the outstanding operation list without calling the delegate.
///
/// Type of the OperationId
///
public class AsyncOperation : AsyncOperation where TResultEventArgs : EventArgs
{
#region Fields
private EventHandler resultDelegate;
private Type resultEventArgsType;
#endregion
#region Construction
///
/// Internal constructor.
///
///
///
///
///
///
///
internal AsyncOperation(object owner, TOperationId id, object userToken, object data, EventHandler resultDelegate, TimeSpan abandonedTime)
: base(owner, id, userToken, data, abandonedTime)
{
this.resultDelegate = resultDelegate;
resultEventArgsType = typeof(TResultEventArgs);
}
#endregion
#region Properties
internal override Type ResultEventArgsType
{
get { return resultEventArgsType; }
}
#endregion
#region Methods
///
/// Completes the operation, calls the result delegate with the specified event args.
///
///
public void Complete(TResultEventArgs resultEventArgs)
{
Complete();
if (resultDelegate != null)
resultDelegate(Owner, resultEventArgs);
}
#endregion
#region Debug methods
///
/// Presents the class as a string.
///
///
public virtual string ToString(string format, IFormatProvider provider)
{
string ownerName;
IIdentifiableEntity identifiableOwner = Owner as IIdentifiableEntity;
if (identifiableOwner != null)
ownerName = IdentifiableEntity.ToString(identifiableOwner);
else
ownerName = this.Owner.ToString();
return string.Format(System.Globalization.CultureInfo.InvariantCulture, "AsyncOperation Owner={2},Id={3},",
typeof(TOperationId).FullName, typeof(TResultEventArgs).FullName, ownerName, Id);
}
///
/// Presents the class as a string using the specified culture-specific format information.
///
///
public virtual string ToString(IFormatProvider provider)
{
return ToString("", provider);
}
///
/// Presents the class as a string using a format string.
///
///
public virtual string ToString(string format)
{
return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
}
///
/// Presents the class as a string using a format string and the specified culture-specific format information.
///
///
public override string ToString()
{
return ToString("", System.Globalization.CultureInfo.InvariantCulture);
}
#endregion
}
}