public ITaskResult Finally(Action <Exception> action) { var ret = new ManualThreadPoolResult(); ret.Start(); var task = CreateTask(ret); ThreadPoolResult.OnCompleted(() => { var subResult = CreateResult(() => { action(null); }); subResult.OnCompleted(() => { if (HasResult) { task.Result = Result; } ret.SetCompleted(); }); subResult.OnFailed(error => { ret.SetFailed(error); }); subResult.Start(); }); ThreadPoolResult.OnFailed(error => { var subResult = CreateResult(() => { action(error); }); subResult.OnCompleted(() => { ret.SetFailed(error); }); subResult.OnFailed(nested => { ret.SetFailed(ProcessException(ExceptionUtility.AggregateExceptions("finally handler failed", nested, error))); }); subResult.Start(); }); return(new BoundTaskResult(this, task)); }
public ITaskResult Catch(Action <Exception> action) { var ret = new ManualThreadPoolResult(); ret.Start(); var task = CreateTask(ret); ThreadPoolResult.OnFailed(error => { var subResult = CreateResult(() => { if (!(error is FailureProcessedException)) { action(error); } }); subResult.OnCompleted(() => { if (!(error is FailureProcessedException)) { ret.SetFailed(new FailureProcessedException("failure caught", error)); } else { ret.SetFailed(error); } }); subResult.OnFailed(nested => { ret.SetFailed(ProcessException(ExceptionUtility.AggregateExceptions("catch handler failed", nested, error))); }); subResult.Start(); }); ThreadPoolResult.OnCompleted(() => { if (HasResult) { task.Result = Result; } ret.SetCompleted(); }); return(new BoundTaskResult(this, task)); }
protected Backbone BaseFinally <Backbone>(Action <Exception> handler, Backbone ret) where Backbone : BackboneIntervalImpl { lock (this) { Action <Exception> errorHandler = (err) => { Factory.StartNew(() => { try { handler(err); } catch (Exception inner) { ret.RaiseFailed(ExceptionUtility.AggregateExceptions("failed to execute error handler", err, inner)); } }, Priority); }; _errorHandlers.Add(errorHandler); Action <object> completedHandler = value => { Factory.StartNew(() => { try { handler(null); ret.RaiseCompleted(value); } catch (Exception inner) { ret.RaiseFailed(inner); } }, Priority); }; _completedHandlers.Add(completedHandler); return(ret); } }
protected override void DoStart() { Action convertedAction = new Action(() => { try { lock (SyncRoot) { if (State == AsyncState.Canceled) { return; } } SetRunning(); _action(); lock (SyncRoot) { if (State == AsyncState.Canceled) { return; } } SetCompleted(); } catch (Exception e) { try { SetFailed(e); } catch (Exception nested) { Environment.FailFast("Error occured while calling error handler", ExceptionUtility.AggregateExceptions("Error occured while calling error handler", nested, e)); } } }); DoStart(_backend, convertedAction); }