From eda09f706605e273ecfff7ee30a2f9480d039c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Wed, 3 Jun 2020 21:40:50 +0200 Subject: [PATCH] Refactoring and developing a progressbar on the first async function --- SyncAsyncApp/DemoMethods.cs | 109 +++++++++++++++++++++++ SyncAsyncApp/MainWindow.xaml | 8 +- SyncAsyncApp/MainWindow.xaml.cs | 133 ++++++++++++---------------- SyncAsyncApp/ProgressReportModel.cs | 14 +++ SyncAsyncApp/SyncAsyncApp.csproj | 2 + 5 files changed, 187 insertions(+), 79 deletions(-) create mode 100644 SyncAsyncApp/DemoMethods.cs create mode 100644 SyncAsyncApp/ProgressReportModel.cs diff --git a/SyncAsyncApp/DemoMethods.cs b/SyncAsyncApp/DemoMethods.cs new file mode 100644 index 0000000..1f40b08 --- /dev/null +++ b/SyncAsyncApp/DemoMethods.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace SyncAsyncApp +{ + public static class DemoMethods + { + private static List PrepData() + { + List output = new List(); + + output.Add("https://www.yahoo.com"); + output.Add("https://www.google.com"); + output.Add("https://www.microsoft.com"); + output.Add("https://www.cnn.com"); + output.Add("https://www.amazon.com"); + output.Add("https://www.facebook.com"); + output.Add("https://www.twitter.com"); + output.Add("https://www.codeproject.com"); + output.Add("https://www.stackoverflow.com"); + output.Add("https://en.wikipedia.org/wiki/.NET_Framework"); + + return output; + + } + + public static List RunDownloadSync() + { + List websites = PrepData(); + List output = new List(); + + foreach (string site in websites) + { + WebsiteDataModel results = DownLoadWebSite(site); + output.Add(results); + } + + return output; + + } + + public static async Task> RunDownloadASync(IProgress progress) + { + List websites = PrepData(); + List output = new List(); + ProgressReportModel report = new ProgressReportModel(); + + foreach (string site in websites) + { + WebsiteDataModel results = await DownLoadWebSiteAsync(site); + output.Add(results); + + report.SitesDownoaded = output; + report.PercentageComplete = (output.Count * 100) / websites.Count; + + progress.Report(report); + } + + return output; + } + + public static async Task> RunDownloadParallelASync() + { + + List websites = PrepData(); + List> tasks = new List>(); + + foreach (string site in websites) + { + // Make it async + // tasks.Add( Task.Run(() => DownLoadWebSite(site))); + + //Use Native async + tasks.Add(DownLoadWebSiteAsync(site)); + } + + var results = await Task.WhenAll(tasks); + + return new List(results); + } + + private static async Task DownLoadWebSiteAsync(string websiteURL) + { + WebsiteDataModel output = new WebsiteDataModel(); + WebClient client = new WebClient(); + + output.WebsiteUrl = websiteURL; + output.WebsiteData = await client.DownloadStringTaskAsync(websiteURL); + + return output; + } + + private static WebsiteDataModel DownLoadWebSite(string websiteURL) + { + WebsiteDataModel output = new WebsiteDataModel(); + WebClient client = new WebClient(); + + output.WebsiteUrl = websiteURL; + output.WebsiteData = client.DownloadString(websiteURL); + + return output; + } + + } +} diff --git a/SyncAsyncApp/MainWindow.xaml b/SyncAsyncApp/MainWindow.xaml index 2ad7946..d7a57e3 100644 --- a/SyncAsyncApp/MainWindow.xaml +++ b/SyncAsyncApp/MainWindow.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SyncAsyncApp" mc:Ignorable="d" - Title="MainWindow" Height="500" Width="825" FontSize="16"> + Title="MainWindow" Height="650" Width="825" FontSize="16"> @@ -13,6 +13,8 @@ + + @@ -21,6 +23,8 @@ - + + + diff --git a/SyncAsyncApp/MainWindow.xaml.cs b/SyncAsyncApp/MainWindow.xaml.cs index b04e9ab..b2ec1ec 100644 --- a/SyncAsyncApp/MainWindow.xaml.cs +++ b/SyncAsyncApp/MainWindow.xaml.cs @@ -25,11 +25,14 @@ namespace SyncAsyncApp { InitializeComponent(); } - - private async void executeAsync_Click(object sender, RoutedEventArgs e) + private void executeSync_Click(object sender, RoutedEventArgs e) { + resultsWindow.Text = ""; var watch = System.Diagnostics.Stopwatch.StartNew(); - await RunDownloadASync(); + + var results = DemoMethods.RunDownloadSync(); + PrintResults(results); + watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; @@ -37,101 +40,72 @@ namespace SyncAsyncApp } - private void executeSync_Click(object sender, RoutedEventArgs e) + + private async void executeAsync_Click(object sender, RoutedEventArgs e) { + resultsWindow.Text = ""; + Progress progress = new Progress(); + progress.ProgressChanged += ReportProgress; var watch = System.Diagnostics.Stopwatch.StartNew(); - - RunDownloadSync(); - + + var results = await DemoMethods.RunDownloadASync(progress); + PrintResults(results); + watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; resultsWindow.Text += $"Total execution time: {elapsedMs}"; } - private async Task RunDownloadASync() + private void ReportProgress(object sender, ProgressReportModel e) { - - List websites = PrepData(); - foreach (string site in websites) - { - WebsiteDataModel results = await Task.Run(()=> DownLoadWebSite(site)); - ReportWebSiteInfo(results); - } - + dashboardProgress.Value = e.PercentageComplete; + PrintResults(e.SitesDownoaded); } - private async Task RunDownloadParallelASync() - { - List websites = PrepData(); - List> tasks = new List>(); + //private async Task RunDownloadParallelASync() + //{ - foreach (string site in websites) - { - // Make it async - // tasks.Add( Task.Run(() => DownLoadWebSite(site))); + // List websites = PrepData(); + // List> tasks = new List>(); - //Use Native async - tasks.Add(DownLoadWebSiteAsync(site)); - } + // foreach (string site in websites) + // { + // // Make it async + // // tasks.Add( Task.Run(() => DownLoadWebSite(site))); - var results = await Task.WhenAll(tasks); + // //Use Native async + // tasks.Add(DownLoadWebSiteAsync(site)); + // } - foreach (var item in results) - { - ReportWebSiteInfo(item); - } - } + // var results = await Task.WhenAll(tasks); - private void RunDownloadSync() - { + // foreach (var item in results) + // { + // ReportWebSiteInfo(item); + // } + //} - List websites = PrepData(); - foreach(string site in websites) - { - WebsiteDataModel results = DownLoadWebSite(site); - ReportWebSiteInfo(results); - } - } - private void ReportWebSiteInfo(WebsiteDataModel data) - { - resultsWindow.Text += $"{data.WebsiteUrl} downloaded: {data.WebsiteData.Length} characters long. {Environment.NewLine}"; - } + //private void ReportWebSiteInfo(WebsiteDataModel data) + //{ + // resultsWindow.Text += $"{data.WebsiteUrl} downloaded: {data.WebsiteData.Length} characters long. {Environment.NewLine}"; + //} - private WebsiteDataModel DownLoadWebSite(string websiteURL) - { - WebsiteDataModel output = new WebsiteDataModel(); - WebClient client = new WebClient(); - output.WebsiteUrl = websiteURL; - output.WebsiteData = client.DownloadString(websiteURL); - return output; - } - private List PrepData() - { - List output = new List(); - - resultsWindow.Text = ""; - output.Add("https://www.yahoo.com"); - output.Add("https://www.google.com"); - output.Add("https://www.microsoft.com"); - output.Add("https://www.cnn.com"); - output.Add("https://www.codeproject.com"); - output.Add("https://www.stackoverflow.com"); - - return output; - - } private async void executeParallelAsync_Click(object sender, RoutedEventArgs e) { + resultsWindow.Text = ""; var watch = System.Diagnostics.Stopwatch.StartNew(); - await RunDownloadParallelASync(); + + var results = await DemoMethods.RunDownloadParallelASync(); + PrintResults(results); + watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; @@ -139,16 +113,21 @@ namespace SyncAsyncApp } - private async Task DownLoadWebSiteAsync(string websiteURL) + + + private void executeCancelOperation_Click(object sender, RoutedEventArgs e) { - WebsiteDataModel output = new WebsiteDataModel(); - WebClient client = new WebClient(); - output.WebsiteUrl = websiteURL; - output.WebsiteData = await client.DownloadStringTaskAsync(websiteURL); - - return output; } + private void PrintResults(List results) + { + resultsWindow.Text = ""; + + foreach (var item in results) + { + resultsWindow.Text += $"{item.WebsiteUrl} downloaded: {item.WebsiteData.Length} characters long. {Environment.NewLine}"; + } + } } } diff --git a/SyncAsyncApp/ProgressReportModel.cs b/SyncAsyncApp/ProgressReportModel.cs new file mode 100644 index 0000000..0f7019e --- /dev/null +++ b/SyncAsyncApp/ProgressReportModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SyncAsyncApp +{ + public class ProgressReportModel + { + public int PercentageComplete { get; set; } = 0; + public List SitesDownoaded { get; set; } = new List(); + } +} diff --git a/SyncAsyncApp/SyncAsyncApp.csproj b/SyncAsyncApp/SyncAsyncApp.csproj index 08329bc..f315fdf 100644 --- a/SyncAsyncApp/SyncAsyncApp.csproj +++ b/SyncAsyncApp/SyncAsyncApp.csproj @@ -55,6 +55,8 @@ MSBuild:Compile Designer + + MSBuild:Compile