Ejemplo n.º 1
0
 /// <summary>Marks the task as failed and binds the specified exception to the task.</summary>
 /// <param name="exception">The exception to bind to the task.</param>
 public void SetException(Exception exception)
 {
     _methodBuilder.SetException(exception);
 }
Ejemplo n.º 2
0
        public static IEnumerable<object[]> CanceledTasksAndExpectedCancellationExceptions()
        {
            var cts = new CancellationTokenSource();
            var oce = new OperationCanceledException(cts.Token);

            // Scheduled Task
            Task<int> generic = Task.Run<int>(new Func<int>(() =>
            {
                cts.Cancel();
                throw oce;
            }), cts.Token);
            yield return new object[] { LineNumber(), generic, oce };

            Task nonGeneric = generic;

            // WhenAll Task and Task<int>
            yield return new object[] { LineNumber(), Task.WhenAll(generic), oce };
            yield return new object[] { LineNumber(), Task.WhenAll(generic, Task.FromResult(42)), oce };
            yield return new object[] { LineNumber(), Task.WhenAll(Task.FromResult(42), generic), oce };
            yield return new object[] { LineNumber(), Task.WhenAll(generic, generic, generic), oce };
            yield return new object[] { LineNumber(), Task.WhenAll(nonGeneric), oce };
            yield return new object[] { LineNumber(), Task.WhenAll(nonGeneric, Task.FromResult(42)), oce };
            yield return new object[] { LineNumber(), Task.WhenAll(Task.FromResult(42), nonGeneric), oce };
            yield return new object[] { LineNumber(), Task.WhenAll(nonGeneric, nonGeneric, nonGeneric), oce };

            // Task.Run Task and Task<int> with unwrapping
            yield return new object[] { LineNumber(), Task.Run(() => generic), oce };
            yield return new object[] { LineNumber(), Task.Run(() => nonGeneric), oce };

            // A FromAsync Task and Task<int>
            yield return new object[] { LineNumber(), Task.Factory.FromAsync(generic, new Action<IAsyncResult>(ar => { throw oce; })), oce };
            yield return new object[] { LineNumber(), Task<int>.Factory.FromAsync(nonGeneric, new Func<IAsyncResult, int>(ar => { throw oce; })), oce };

            // AsyncTaskMethodBuilder
            var atmb = new AsyncTaskMethodBuilder();
            atmb.SetException(oce);
            yield return new object[] { LineNumber(), atmb.Task, oce };
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     Marks the task as failed and binds the specified exception to the task.
 /// </summary>
 /// <param name="exception">The exception to bind to the task.</param>
 /// <exception cref="T:System.ArgumentNullException"><paramref name="exception" /> is null.</exception>
 /// <exception cref="T:System.InvalidOperationException">The task has already completed.-or-The builder is not initialized.</exception>
 public void SetException(Exception exception)
 {
     m_builder.SetException(exception);
 }
 public void SetException(Exception exception)
 {
     _useBuilder = true;
     _methodBuilder.SetException(exception);
 }
        public static void RunAsyncMethodBuilderTests_NegativeTests()
        {
            // Incorrect usage for AsyncVoidMethodBuilder
            {
                var atmb = new AsyncTaskMethodBuilder();
                Assert.Throws<ArgumentNullException>(
                   () => { atmb.SetException(null); });
            }

            // Incorrect usage for AsyncTaskMethodBuilder
            {
                var avmb = AsyncVoidMethodBuilder.Create();
                Assert.Throws<ArgumentNullException>(
                 () => { avmb.SetException(null); });
            }

            // Creating a task builder, building it, completing it successfully, and making sure it can't be reset
            {
                var atmb = AsyncTaskMethodBuilder.Create();
                atmb.SetResult();
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetResult(); });
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetException(new Exception()); });
            }

            // Incorrect usage for AsyncTaskMethodBuilder<T>
            {
                var atmb = new AsyncTaskMethodBuilder<int>();
                Assert.Throws<ArgumentNullException>(
                   () => { atmb.SetException(null); });
            }

            // Creating a task builder <T>, building it, completing it successfully, and making sure it can't be reset
            {
                var atmb = AsyncTaskMethodBuilder<int>.Create();
                atmb.SetResult(43);
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetResult(44); });
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetException(new Exception()); });
            }
        }
Ejemplo n.º 6
0
 /// <summary>Marks the task as failed and binds the specified exception to the task.</summary>
 /// <param name="exception">The exception to bind to the task.</param>
 public void SetException(Exception exception) =>
 AsyncTaskMethodBuilder <VoidTaskResult> .SetException(exception, ref m_task);