Ejemplo n.º 1
0
        public static void HandleExceptions <T>(this ValueTask <T> valueTask, ILogger?logger = null)
        {
            if (valueTask.IsCompletedSuccessfully)
            {
                return;
            }

            if (valueTask.IsCompleted)
            {
                try
                {
                    valueTask.GetAwaiter().GetResult();
                }
#pragma warning disable CA1031
                catch (Exception exc)
#pragma warning restore CA1031
                {
                    ExceptionHelper.LogException(exc, logger);
                }

                return;
            }

            valueTask.AsTask().HandleExceptions(logger);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ValueTaskAwaiter"/> struct.
 /// </summary>
 internal ValueTaskAwaiter(ref SystemValueTask awaitedTask)
 {
     this.AwaitedTask = ValueTaskAwaiter.TryGetTask(ref awaitedTask, out Task innerTask) ?
                        innerTask : null;
     this.Awaiter = awaitedTask.GetAwaiter();
     RuntimeProvider.TryGetFromSynchronizationContext(out CoyoteRuntime runtime);
     this.Runtime = runtime;
 }
        public static IEnumerable <T> ToBlockingEnumerable <T>(this IAsyncEnumerable <T> source, CancellationToken cancellationToken = default)
        {
            IAsyncEnumerator <T> enumerator = source.GetAsyncEnumerator(cancellationToken);
            // A ManualResetEventSlim variant that lets us reuse the same
            // awaiter callback allocation across the entire enumeration.
            ManualResetEventWithAwaiterSupport?mres = null;

            try
            {
                while (true)
                {
#pragma warning disable CA2012 // Use ValueTasks correctly
                    ValueTask <bool> moveNextTask = enumerator.MoveNextAsync();
#pragma warning restore CA2012 // Use ValueTasks correctly

                    if (!moveNextTask.IsCompleted)
                    {
                        (mres ??= new()).Wait(moveNextTask.ConfigureAwait(false).GetAwaiter());
                        Debug.Assert(moveNextTask.IsCompleted);
                    }

                    if (!moveNextTask.Result)
                    {
                        yield break;
                    }

                    yield return(enumerator.Current);
                }
            }
            finally
            {
                ValueTask disposeTask = enumerator.DisposeAsync();

                if (!disposeTask.IsCompleted)
                {
                    (mres ?? new()).Wait(disposeTask.ConfigureAwait(false).GetAwaiter());
                    Debug.Assert(disposeTask.IsCompleted);
                }

                disposeTask.GetAwaiter().GetResult();
            }
        }
Ejemplo n.º 4
0
 public static void Wait <T>(this ValueTask <T> task)
 => task.GetAwaiter().GetResult();