namespace Caliburn.Micro.Core {
    using System;
    /// 
    /// A result that executes an .
    /// 
    public class DelegateResult : IResult {
        readonly Action toExecute;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The action.
        public DelegateResult(Action action) {
            toExecute = action;
        }
        /// 
        /// Executes the result using the specified context.
        /// 
        /// The context.
        public void Execute(CoroutineExecutionContext context) {
            var eventArgs = new ResultCompletionEventArgs();
            try {
                toExecute();
            }
            catch (Exception ex) {
                eventArgs.Error = ex;
            }
            Completed(this, eventArgs);
        }
        /// 
        /// Occurs when execution has completed.
        /// 
        public event EventHandler Completed = delegate { };
    }
    /// 
    /// A result that executes a 
    /// 
    /// The type of the result.
    public class DelegateResult : IResult {
        readonly Func toExecute;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The action.
        public DelegateResult(Func action) {
            toExecute = action;
        }
        /// 
        /// Executes the result using the specified context.
        /// 
        /// The context.
        public void Execute(CoroutineExecutionContext context) {
            var eventArgs = new ResultCompletionEventArgs();
            try {
                Result = toExecute();
            }
            catch (Exception ex) {
                eventArgs.Error = ex;
            }
            Completed(this, eventArgs);
        }
        /// 
        /// Gets the result.
        /// 
        public TResult Result { get; private set; }
        /// 
        /// Occurs when execution has completed.
        /// 
        public event EventHandler Completed = delegate { };
    }
}