public static void PreCanceledToken_ForAll()
        {
            OperationCanceledException caughtException = null;
            var cs = new CancellationTokenSource();

            cs.Cancel();

            IEnumerable <int> throwOnFirstEnumerable = Enumerables <int> .ThrowOnEnumeration();

            try
            {
                throwOnFirstEnumerable
                .AsParallel()
                .WithCancellation(cs.Token)
                .ForAll((x) => { Console.WriteLine(x.ToString()); });
            }
            catch (OperationCanceledException ex)
            {
                caughtException = ex;
            }

            Assert.NotNull(caughtException);
            Assert.Equal(cs.Token, caughtException.CancellationToken);
        }
Ejemplo n.º 2
0
 // Return an enumerable which throws on first MoveNext.
 // Useful for testing promptness of cancellation.
 public static IEnumerable <object[]> ThrowOnFirstEnumeration()
 {
     yield return(new object[] { Labeled.Label("ThrowOnFirstEnumeration", Enumerables <int> .ThrowOnEnumeration().AsParallel()), 8 });
 }