Beispiel #1
0
        // Runs parallel tasks, but time adds up
        private async void AsyncBtn_Click(object sender, RoutedEventArgs e)
        {
            ClearOutput();
            DisplayTaskDetails("Async Task Running...\n");

            var watch = System.Diagnostics.Stopwatch.StartNew();

            var output = await RunAsyncDemo.Start();

            RenderListOutput(output);

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            DisplayTaskDetails("Async Task Stopped...\n");
            DisplayTaskDetails($"Elapsed Time: {elapsedMs}\n");
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Fetching data - SYNC!");

            var watch  = System.Diagnostics.Stopwatch.StartNew();
            var output = RunSyncDemo.Start();

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine($"Total execution time: { elapsedMs }");

            Console.WriteLine("========================");

            Console.WriteLine("Fetching data - ASYNC!");

            // Main returns to the OS - so your program exits.
            Task.Run(async() =>
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();

                var output = await RunAsyncDemo.Start();

                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine($"Total execution time: { elapsedMs }");
            });

            // As a workaround to the above, we can provide our own async-compatible context
            try
            {
                AsyncContext.Run(() => RunAsyncDemo.Start());
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
        }