Esempio n. 1
0
        public static Task WaitAsync([NotNull] this IAwaitable AAwaitable)
        {
            Require.NotNull(AAwaitable, nameof(AAwaitable));

            // NOTE: The cancellation token is useless and can neither canceled nor be disposed.
            // ReSharper disable once ExceptionNotDocumented
            return(AAwaitable.WaitAsync(CancellationToken.None));
        }
Esempio n. 2
0
        /// <summary>
        /// Get an awaiter for this <see cref="IAwaitable{TResult}"/>.
        /// </summary>
        /// <typeparam name="TResult">The awaitable result type.</typeparam>
        /// <param name="AAwaitable">The <see cref="IAwaitable{TResult}"/>.</param>
        /// <returns>An awaiter for <paramref name="AAwaitable"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="AAwaitable"/> is <see langword="null"/>.
        /// </exception>
        public static TaskAwaiter <TResult> GetAwaiter <TResult>(
            [NotNull] this IAwaitable <TResult> AAwaitable
            )
        {
            Require.NotNull(AAwaitable, nameof(AAwaitable));

            return(AAwaitable.WaitAsync().GetAwaiter());
        }
Esempio n. 3
0
        /// <summary>
        /// Synchronously wait for the event.
        /// </summary>
        /// <param name="AAwaitable">The <see cref="IAwaitable"/>.</param>
        /// <param name="ACancel">A <see cref="CancellationToken"/> to cancel the wait.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="AAwaitable"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="OperationCanceledException">The wait was canceled.</exception>
        /// <exception cref="ObjectDisposedException">
        /// The <see cref="CancellationTokenSource"/> of <paramref name="ACancel"/> is disposed.
        /// </exception>
        /// <exception cref="Exception">
        /// Any <see cref="Exception"/> thrown by <paramref name="AAwaitable"/>.
        /// </exception>
        /// <remarks>
        /// If required, internal exceptions that were wrapped in a <see cref="AggregateException"/>
        /// are unwrapped and rethrown.
        /// </remarks>
        public static void Wait([NotNull] this IAwaitable AAwaitable, CancellationToken ACancel)
        {
            Require.NotNull(AAwaitable, nameof(AAwaitable));

            try
            {
                AAwaitable.WaitAsync(ACancel).Wait(ACancel);
            }
            catch (AggregateException error)
            {
                // Throw the first aggregated exception.
                throw error.InnerException ?? error;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Synchronously wait for the result.
        /// </summary>
        /// <typeparam name="TResult">The awaitable result type.</typeparam>
        /// <param name="AAwaitable">The <see cref="IAwaitable{TResult}"/>.</param>
        /// <param name="ACancel">A <see cref="CancellationToken"/> to cancel the wait.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="AAwaitable"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="OperationCanceledException">The wait was canceled.</exception>
        /// <exception cref="ObjectDisposedException">
        /// The <see cref="CancellationTokenSource"/> of <paramref name="ACancel"/> is disposed.
        /// </exception>
        /// <exception cref="Exception">
        /// Any <see cref="Exception"/> thrown by <paramref name="AAwaitable"/>.
        /// </exception>
        /// <remarks>
        /// If required, internal exceptions that were wrapped in a <see cref="AggregateException"/>
        /// are unwrapped and rethrown.
        /// </remarks>
        public static TResult Wait <TResult>(
            [NotNull] this IAwaitable <TResult> AAwaitable,
            CancellationToken ACancel
            )
        {
            Require.NotNull(AAwaitable, nameof(AAwaitable));

            try
            {
                var task = AAwaitable.WaitAsync(ACancel);
                task.Wait(ACancel);
                return(task.Result);
            }
            catch (AggregateException error)
            {
                // Throw the first aggregated exception.
                throw error.InnerException ?? error;
            }
        }