Ejemplo n.º 1
0
            public async Task Run(AsyncTestRunner runner)
            {
                try
                {
                    await _method(runner._ct.Token, runner);

                    _task.TrySetResult(Unit.Default);
                }
                catch (Exception e)
                {
                    _task.TrySetException(e);

                    throw;
                }
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Awaits a task execution on the specified scheduler, providing the result.
        /// </summary>
        /// <returns>A task that will provide the result of the execution.</returns>
        public static Task <T> Run <T>(this IScheduler source, Func <CancellationToken, Task <T> > taskBuilder, CancellationToken cancellationToken)
        {
            var completion = new FastTaskCompletionSource <T>();

            var disposable = new SingleAssignmentDisposable();
            var ctr        = default(CancellationTokenRegistration);

            if (cancellationToken.CanBeCanceled)
            {
                ctr = cancellationToken.Register(() =>
                {
                    completion.TrySetCanceled();
                    disposable.Dispose();
                });
            }

            disposable.Disposable = source.Schedule(
                async() =>
            {
                try
                {
                    var result = await taskBuilder(cancellationToken);
                    completion.TrySetResult(result);
                }
                catch (Exception e)
                {
                    completion.TrySetException(e);
                }
                finally
                {
                    ctr.Dispose();
                }
            }
                );

            return(completion.Task);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Sets the current operation as complete.
 /// </summary>
 internal void Complete()
 {
     CompletionSource?.TrySetResult(null);
     _completedHandler?.Invoke(this, Status);
 }
Ejemplo n.º 4
0
 public void Reached(int position) => Task.Run(() => _task.TrySetResult(position));             // Ensure that awaiter does not run on this thread!