Example #1
0
        static void Main()
        {
            Console.WriteLine("Hello TPL!");
            var sw = new Stopwatch();

            sw.Restart();

            var cts  = new CancellationTokenSource(5000);
            var task = Primes.FromRangeAsync(1, 200_000, cts.Token);

            try
            {
                var primes = task.Result;
                sw.Stop();
                var elapsed = sw.Elapsed;
                Console.WriteLine($"Found {primes.Count} primes after {elapsed}");
            }
            catch (Exception e)
            {
                // ! Exception property is null for cancelled task !
                Console.WriteLine($"Exception '{e}'.");
                Console.WriteLine($"Task exception '{task.Exception}'.");
                Console.WriteLine($"Task status {task.Status}.");
            }

            cts.Dispose();
        }
        private static async Task WorkloadAsync()
        {
            await Primes.FromRangeAsync(1, 100_000, CancellationToken.None);

            Countdown.Signal();
            await Task.Delay(10_000);

            Primes.FromRange(1, 1_000);
        }
Example #3
0
        private static async Task <List <int> > FromRangeAsync(int from, int to)
        {
            Console.WriteLine($"1.Thread Id: {Thread.CurrentThread.ManagedThreadId}");
            await Task.Delay(1000).ConfigureAwait(false);

            Console.WriteLine($"2.Thread Id: {Thread.CurrentThread.ManagedThreadId}");
            await Task.Delay(1000);

            Console.WriteLine($"3.Thread Id: {Thread.CurrentThread.ManagedThreadId}");
            var result = await Primes.FromRangeAsync(from, to, CancellationToken.None);

            Console.WriteLine($"4.Thread Id: {Thread.CurrentThread.ManagedThreadId}");
            return(result);
        }
        static void Main()
        {
            Console.WriteLine("Hello TPL!");
            var sw = new Stopwatch();

            sw.Restart();

            Task <List <int> > task   = Primes.FromRangeAsync(7, 7, CancellationToken.None);
            List <int>         primes = task.Result;

            sw.Stop();
            var elapsed = sw.Elapsed;

            Console.WriteLine($"Found {primes.Count} primes after {elapsed}");
        }
        static async Task Main()
        {
            Console.WriteLine("Hello Async!");
            var sw = new Stopwatch();

            sw.Restart();
            var ct = CancellationToken.None;

            List <int> primes1 = await Primes.FromRangeAsync(1, 100_000, ct);

            List <int> primes2 = await Primes.FromRangeAsync(100_000, 150_000, ct);

            List <int> primes3 = await Primes.FromRangeAsync(150_000, 200_000, ct);

            int primes = primes1.Count + primes2.Count + primes3.Count;

            sw.Stop();
            var elapsed = sw.Elapsed;

            Console.WriteLine($"Found {primes} primes after {elapsed}");
        }
        static async Task Main()
        {
            Console.WriteLine("Hello Async!");
            var sw = new Stopwatch();

            sw.Restart();
            var ct = CancellationToken.None;

            Task <List <int> > task1 = Primes.FromRangeAsync(1, 100_000, ct);
            Task <List <int> > task2 = Primes.FromRangeAsync(100_000, 150_000, ct);
            Task <List <int> > task3 = Primes.FromRangeAsync(150_000, 200_000, ct);

            List <int>[] results = await Task.WhenAll(task1, task2, task3);

            int primes = results.Sum(r => r.Count);

            sw.Stop();
            var elapsed = sw.Elapsed;

            Console.WriteLine($"Found {primes} primes after {elapsed}");
        }