Example #1
0
        /// <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 awaiter = new DynamicTaskAwaitable.Awaiter(task, false);
                result = new(awaiter.GetRawResult());
            }
            catch (Exception e)
            {
                result = new(e);
            }

            return(result);
        }
Example #2
0
        /// <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 <dynamic?> result;

            try
            {
                if (task.Wait(timeout))
                {
                    var awaiter = new DynamicTaskAwaitable.Awaiter(task, false);
                    result = new(awaiter.GetRawResult());
                }
                else
                {
                    result = new(new TimeoutException());
                }
            }
            catch (Exception e)
            {
                result = new(e);
            }

            return(result);
        }