/// <summary> /// Gets task result synchronously. /// </summary> /// <param name="task">The task to synchronize.</param> /// <param name="token">Cancellation token.</param> /// <returns>Task result; or <see cref="System.Reflection.Missing.Value"/> returned from <see cref="Result{T}.Value"/> if <paramref name="task"/> is not of type <see cref="Task{TResult}"/>.</returns> public static Result <dynamic> GetResult(this Task task, CancellationToken token) { Result <object> result; try { task.Wait(token); var value = DynamicTaskAwaitable.GetResult(task); result = new Result <object>(Unsafe.As <dynamic, object>(ref value)); } catch (Exception e) { result = new Result <object>(e); } return(result); }
/// <summary> /// Gets task result synchronously. /// </summary> /// <param name="task">The task to synchronize.</param> /// <param name="timeout">Synchronization timeout.</param> /// <returns>Task result; or <see cref="System.Reflection.Missing.Value"/> returned from <see cref="Result{T}.Value"/> if <paramref name="task"/> is not of type <see cref="Task{TResult}"/>.</returns> /// <exception cref="TimeoutException">Task is not completed.</exception> public static Result <dynamic> GetResult(this Task task, TimeSpan timeout) { Result <object> result; try { if (task.Wait(timeout)) { var value = DynamicTaskAwaitable.GetResult(task); result = new Result <object>(Unsafe.As <dynamic, object>(ref value)); } else { result = new Result <object>(new TimeoutException()); } } catch (Exception e) { result = new Result <object>(e); } return(result); }
/// <summary> /// Gets dynamically typed task result. /// </summary> /// <returns>The result of the completed task; or <see cref="System.Reflection.Missing.Value"/> if underlying task is not of type <see cref="Task{TResult}"/>.</returns> public dynamic GetResult() => DynamicTaskAwaitable.GetResult(task);