Ejemplo n.º 1
0
        private async void executeAsync_Click(object sender, RoutedEventArgs e)
        {
            resultsWindow.Text = string.Empty;
            resultsWindow.Refresh();
            progressBar.Value = 0;

            var progress = new Progress <ProgressReportModel>();

            progress.ProgressChanged += ReportProgress;

            var watch = Stopwatch.StartNew();

            try
            {
                cancellationTokenSource.Dispose();
                cancellationTokenSource = new CancellationTokenSource();
                var results = await AsyncMethods.RunDownloadAsync(progress, cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                resultsWindow.Text += $"The async download was cancelled {Environment.NewLine}";
            }

            watch.Stop();

            resultsWindow.Text += $"Total execution time: {watch.ElapsedMilliseconds}";
        }
Ejemplo n.º 2
0
        private async void executeAsyncInParallel_Click(object sender, RoutedEventArgs e)
        {
            resultsWindow.Text = string.Empty;
            resultsWindow.Refresh();
            progressBar.Value = 0;

            var watch = Stopwatch.StartNew();

            var results = await AsyncMethods.RunDownloadAsyncInParallel();

            ReportResults(results);

            watch.Stop();

            resultsWindow.Text += $"Total execution time: {watch.ElapsedMilliseconds}";
        }
Ejemplo n.º 3
0
        public static async Task <List <WebsiteDataModel> > RunDownloadAsync(IProgress <ProgressReportModel> progress, CancellationToken cancellationToken)
        {
            var testWebsites = CommonMethods.GetTestWebsites();
            var results      = new List <WebsiteDataModel>();
            var report       = new ProgressReportModel();

            foreach (var website in testWebsites)
            {
                // this allows the UI to respond and be moved but the "await" means that it still does each call one after the other
                // asynchronous calls made synchronously
                var result = await AsyncMethods.DownloadWebsiteAsync(website);

                results.Add(result);

                cancellationToken.ThrowIfCancellationRequested();

                report.WebsitesDownloaded = results;
                report.PercentageComplete = (results.Count * 100) / testWebsites.Count;
                progress.Report(report);
            }

            return(results);
        }