/// <inheritdoc /> protected sealed override void Execute( UIApplication app, TParameter parameter, IExternalEventResultHandler <TResult> resultHandler) { resultHandler.Wait(() => Handle(app, parameter)); }
/// <summary> /// Await a <see cref="Task{TResult}"/> /// </summary> /// <typeparam name="TResult">The type of the result</typeparam> /// <param name="resultHandler"></param> /// <param name="task">The task to wait</param> public static async void Await <TResult>( this IExternalEventResultHandler <TResult> resultHandler, Task <TResult> task) { try { var result = await task; if (task.IsCompleted) { resultHandler.SetResult(result); } if (task.IsCanceled) { resultHandler.Cancel(); } if (task.IsFaulted) { resultHandler.ThrowException(task.Exception ?? new Exception("Unknown Exception")); } } catch (Exception e) { resultHandler.ThrowException(e); } }
/// <inheritdoc /> public Task <TResult> Prepare(TParameter parameter) { Parameter = parameter; var tcs = new TaskCompletionSource <TResult>(); ResultHandler = new DefaultResultHandler <TResult>(tcs); return(tcs.Task); }
/// <summary> /// Wait for an sync delegate to complete /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="resultHandler"></param> /// <param name="function"></param> public static void Wait <TResult>(this IExternalEventResultHandler <TResult> resultHandler, Func <TResult> function) { try { var result = function(); resultHandler.SetResult(result); } catch (Exception e) { resultHandler.ThrowException(e); } }
/// <summary> /// Await a <see cref="Task{TResult}"/> /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="resultHandler"></param> /// <param name="source"></param> /// <returns></returns> public static IExternalEventResultHandler <TResult> Await <TResult>( this IExternalEventResultHandler <TResult> resultHandler, Task <TResult> source) { source.ContinueWith(task => { if (task.IsCompleted) { resultHandler.SetResult(task.Result); } else if (task.IsFaulted) { resultHandler.ThrowException(task.Exception ?? new Exception("Unknown Exception")); } else if (task.IsCanceled) { resultHandler.Cancel(); } }); return(resultHandler); }
/// <summary> /// Override this method to execute some business code /// </summary> /// <param name="app">The revit top-level object, <see cref="UIApplication" /></param> /// <param name="parameter">The parameter passed in</param> /// <param name="resultHandler">The result handler</param> protected abstract void Execute(UIApplication app, TParameter parameter, IExternalEventResultHandler <TResult> resultHandler);