Example #1
0
        public static void PreCanceledToken_SimpleEnumerator()
        {
            OperationCanceledException caughtException = null;
            var cs = new CancellationTokenSource();

            cs.Cancel();

            int[] srcEnumerable = Enumerable.Range(0, 1000).ToArray();
            ThrowOnFirstEnumerable <int> throwOnFirstEnumerable = new ThrowOnFirstEnumerable <int>(srcEnumerable);

            try
            {
                var query = throwOnFirstEnumerable
                            .AsParallel()
                            .WithCancellation(cs.Token);

                foreach (var item in query)
                {
                }
            }
            catch (OperationCanceledException ex)
            {
                caughtException = ex;
            }

            Assert.NotNull(caughtException);
            Assert.Equal(cs.Token, caughtException.CancellationToken);
        }
Example #2
0
        public static void PreCanceledToken_ForAll()
        {
            OperationCanceledException caughtException = null;
            var cs = new CancellationTokenSource();

            cs.Cancel();

            int[] srcEnumerable = Enumerable.Range(0, 1000).ToArray();
            ThrowOnFirstEnumerable <int> throwOnFirstEnumerable = new ThrowOnFirstEnumerable <int>(srcEnumerable);

            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);
        }