public static Task <bool> StartAsAsync(this ITask task) { var tcs = new TaskCompletionSource <bool>(); task.Finally(() => tcs.TrySetResult(true)); task.Catch(e => tcs.TrySetException(e)); task.Start(); return(tcs.Task); }
public static Task <T> StartAsAsync <T>(this ITask <T> task) { var tcs = new TaskCompletionSource <T>(); task.Finally(r => tcs.TrySetResult(r)); task.Catch(e => tcs.TrySetException(e)); task.Start(); return(tcs.Task); }
public ITask Queue(ITask task) { // if this task fails, both OnEnd and Catch will be called // if a task before this one on the chain fails, only Catch will be called // so avoid calling TaskFinished twice by ignoring failed OnEnd calls task.OnEnd += InvokeFinishOnlyOnSuccess; task.Catch(e => TaskFinished(false, e)); queuedTasks.Add(task); return(this); }
public void can_reject_simple_task() { var ex = new Exception(); ITask task = Task.FromError(ex); var errors = 0; task.Catch(e => { Assert.AreEqual(ex, e); ++errors; }); Assert.AreEqual(1, errors); }
public void combined_task_is_rejected_when_both_tasks_are_rejected() { var task1 = new Task <int>(); var task2 = new Task <int>(); ITask <int[]> all = Task.All(task1, task2); all.Then(v => { throw new ApplicationException("Shouldn't happen"); }); var errors = 0; all.Catch(e => { ++errors; }); task1.Reject(new ApplicationException("Error!")); task2.Reject(new ApplicationException("Error!")); Assert.AreEqual(1, errors); }
private ITask HookupHandlers(ITask task, bool filesystemChangesExpected) { var isExclusive = task.IsChainExclusive(); task.GetTopOfChain().OnStart += t => { if (isExclusive) { IsBusy = true; } if (filesystemChangesExpected) { watcher.Stop(); } }; task.OnEnd += (_, __, ___) => { if (filesystemChangesExpected) { //Logger.Trace("Ended Operation - Enable Watcher"); watcher.Start(); } if (isExclusive) { //Logger.Trace("Ended Operation - Clearing Busy Flag"); IsBusy = false; } }; task.Catch(_ => { if (filesystemChangesExpected) { //Logger.Trace("Ended Operation - Enable Watcher"); watcher.Start(); } if (isExclusive) { //Logger.Trace("Ended Operation - Clearing Busy Flag"); IsBusy = false; } }); return(task); }
/// <summary> /// Helper that starts an <see cref="ITask"/> and returns a <see cref="Task"/>, capturing exceptions. If you /// want to await an <see cref="ITask"/>, use this method. /// </summary> /// <param name="task"></param> /// <returns></returns> public static Task <bool> StartAsAsync(this ITask task) { task.EnsureNotNull(nameof(task)); var tcs = new TaskCompletionSource <bool>(); task.FinallyInline(success => { tcs.TrySetResult(success); }); task.Catch(e => { if (e is AggregateException) { e = e.GetBaseException() ?? e; } tcs.TrySetException(e); }); task.Start(); return(tcs.Task); }