Refactoring and developing a progressbar on the first async function

This commit is contained in:
2020-06-03 21:40:50 +02:00
parent 2280d1dea6
commit eda09f7066
5 changed files with 187 additions and 79 deletions

109
SyncAsyncApp/DemoMethods.cs Normal file
View File

@ -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<string> PrepData()
{
List<string> output = new List<string>();
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<WebsiteDataModel> RunDownloadSync()
{
List<string> websites = PrepData();
List<WebsiteDataModel> output = new List<WebsiteDataModel>();
foreach (string site in websites)
{
WebsiteDataModel results = DownLoadWebSite(site);
output.Add(results);
}
return output;
}
public static async Task<List<WebsiteDataModel>> RunDownloadASync(IProgress<ProgressReportModel> progress)
{
List<string> websites = PrepData();
List<WebsiteDataModel> output = new List<WebsiteDataModel>();
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<List<WebsiteDataModel>> RunDownloadParallelASync()
{
List<string> websites = PrepData();
List<Task<WebsiteDataModel>> tasks = new List<Task<WebsiteDataModel>>();
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<WebsiteDataModel>(results);
}
private static async Task<WebsiteDataModel> 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;
}
}
}

View File

@ -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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
@ -13,6 +13,8 @@
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" Margin="10 0" FontSize="28">
@ -21,6 +23,8 @@
<Button x:Name="executeSync" Grid.Row="2" Margin="10" Padding="5" Click="executeSync_Click">Normal Execute</Button>
<Button x:Name="executeAsync" Grid.Row="3" Margin="10" Padding="5" Click="executeAsync_Click">Async Execute</Button>
<Button x:Name="executeParaAsync" Grid.Row="4" Margin="10" Padding="5" Click="executeParallelAsync_Click">Parallel Async Execute</Button>
<TextBlock x:Name="resultsWindow" Grid.Row="5" Margin="10"/>
<Button x:Name="cancelOperation" Grid.Row="5" Margin="10" Padding="5" Click="executeCancelOperation_Click">Cancel Operation</Button>
<ProgressBar x:Name="dashboardProgress" Grid.Row="6" Margin="10" Height="25" />
<TextBlock x:Name="resultsWindow" Grid.Row="7" Margin="10"/>
</Grid>
</Window>

View File

@ -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<ProgressReportModel> progress = new Progress<ProgressReportModel>();
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<string> 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<string> websites = PrepData();
List<Task<WebsiteDataModel>> tasks = new List<Task<WebsiteDataModel>>();
//private async Task RunDownloadParallelASync()
//{
foreach (string site in websites)
{
// Make it async
// tasks.Add( Task.Run(() => DownLoadWebSite(site)));
// List<string> websites = PrepData();
// List<Task<WebsiteDataModel>> tasks = new List<Task<WebsiteDataModel>>();
//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<string> 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<string> PrepData()
{
List<string> output = new List<string>();
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<WebsiteDataModel> 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<WebsiteDataModel> results)
{
resultsWindow.Text = "";
foreach (var item in results)
{
resultsWindow.Text += $"{item.WebsiteUrl} downloaded: {item.WebsiteData.Length} characters long. {Environment.NewLine}";
}
}
}
}

View File

@ -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<WebsiteDataModel> SitesDownoaded { get; set; } = new List<WebsiteDataModel>();
}
}

View File

@ -55,6 +55,8 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="DemoMethods.cs" />
<Compile Include="ProgressReportModel.cs" />
<Compile Include="WebsiteDataModel.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>