/// <summary> /// Sets Waiter, then waits until the operation is canceled using the ISchedulerAction. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset?lastExecuted, ISchedulerAction operation) { mWaiter.Set(); SpinWait.SpinUntil(() => { return(operation.IsCancellationRequested); }); throw new OperationCanceledException(); }
/// <summary> /// Sets Waiter, then waits until the operation is canceled using the ISchedulerAction. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset? lastExecuted, ISchedulerAction operation) { mWaiter.Set(); SpinWait.SpinUntil(() => { return operation.IsCancellationRequested; }); throw new OperationCanceledException(); }
private void OnTaskCompleted(ScheduledTask <T> task, ISchedulerAction action) { try { TaskCompleted?.Invoke(this, new ScheduledTaskCompletedEventArgs <T>(task, action.IsSuccess, action.IsCanceled, action.Exception, action.State)); } catch { } }
/// <summary> /// Executes the <see cref="Action"/> of the <see cref="ScheduledTask{T}"/>. /// </summary> /// <param name="operation">Contains the execution state and results.</param> /// <exception cref="CodedArgumentNullException"><paramref name="operation"/> is <see langword="null"/>.</exception> public void Run(ISchedulerAction operation) { if (operation == null) { return; } DateTimeOffset NewStartTime = DateTimeOffset.Now; try { AssertActive(); mIsActive = true; operation.State = mAction.Run(mLastStartTime, operation); if (operation.IsCancellationRequested) { operation.IsCanceled = true; operation.IsSuccess = false; } else { operation.IsSuccess = true; } } catch (Exception ex) { if (ex is OperationCanceledException) { operation.IsCanceled = true; } #if WINDOWS_DESKTOP else if (ex is ThreadAbortException) { operation.IsCanceled = true; } #endif operation.IsSuccess = false; operation.Exception = ex; } finally { if (!operation.IsCanceled) { mLastStartTime = NewStartTime; } if (operation.IsSuccess) { mLastEndTime = DateTimeOffset.Now; } else { mLastEndTime = null; } mIsActive = false; } }
/// <summary> /// Increments a counter, and sets Waiter when the counter is equal to Iterations. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset?lastExecuted, ISchedulerAction operation) { IterationsExecuted++; if (IterationsExecuted == mIterations) { mWaiter.Set(); } return(null); }
/// <summary> /// Depending on Action, Run either completes or throws OperationCanceledExceptions or InvalidOperationExceptions. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset?lastExecuted, ISchedulerAction operation) { mLastRun = DateTimeOffset.Now; if (mAction == DummyAction.Cancel) { throw new OperationCanceledException(); } else if (mAction == DummyAction.ThrowException) { throw new InvalidOperationException(); } return(42); }
/// <summary> /// When implemented by a deriving class, executes the task that was scheduled. /// </summary> /// <param name="lastExecuted">The date and time of the last execution of the action.</param> /// <param name="operation">Represents the asynchronous operation that the method can observe while executed.</param> /// <returns>A state object, or <see langword="null"/>.</returns> /// <remarks>Use <paramref name="operation"/> to check if the method should be canceled, and throw <see cref="OperationCanceledException"/> to notify the cancellation.</remarks> public abstract object Run(DateTimeOffset? lastExecuted, ISchedulerAction operation);
/// <summary> /// Depending on Action, Run either completes or throws OperationCanceledExceptions or InvalidOperationExceptions. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset? lastExecuted, ISchedulerAction operation) { mLastRun = DateTimeOffset.Now; if (mAction == DummyAction.Cancel) { throw new OperationCanceledException(); } else if (mAction == DummyAction.ThrowException) { throw new InvalidOperationException(); } return 42; }
/// <summary> /// Increments a counter, and sets Waiter when the counter is equal to Iterations. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset?lastExecuted, ISchedulerAction operation) { mStartWaiter.Set(); mWaiter.WaitOne(); return(null); }
/// <summary> /// Increments a counter, and sets Waiter when the counter is equal to Iterations. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset? lastExecuted, ISchedulerAction operation) { mStartWaiter.Set(); mWaiter.WaitOne(); return null; }
/// <summary> /// When implemented by a deriving class, executes the task that was scheduled. /// </summary> /// <param name="lastExecuted">The date and time of the last execution of the action.</param> /// <param name="operation">Represents the asynchronous operation that the method can observe while executed.</param> /// <returns>A state object, or <see langword="null"/>.</returns> /// <remarks>Use <paramref name="operation"/> to check if the method should be canceled, and throw <see cref="OperationCanceledException"/> to notify the cancellation.</remarks> public abstract object Run(DateTimeOffset?lastExecuted, ISchedulerAction operation);
/// <summary> /// Increments a counter, and sets Waiter when the counter is equal to Iterations. /// </summary> /// <param name="lastExecuted"></param> /// <param name="operation"></param> /// <returns></returns> public override object Run(DateTimeOffset? lastExecuted, ISchedulerAction operation) { IterationsExecuted++; if (IterationsExecuted == mIterations) { mWaiter.Set(); } return null; }