Refactoring and developing a progressbar on the first async function
This commit is contained in:
109
SyncAsyncApp/DemoMethods.cs
Normal file
109
SyncAsyncApp/DemoMethods.cs
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,7 +5,7 @@
|
|||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:local="clr-namespace:SyncAsyncApp"
|
xmlns:local="clr-namespace:SyncAsyncApp"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
Title="MainWindow" Height="500" Width="825" FontSize="16">
|
Title="MainWindow" Height="650" Width="825" FontSize="16">
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<RowDefinition Height="auto" />
|
<RowDefinition Height="auto" />
|
||||||
@ -13,6 +13,8 @@
|
|||||||
<RowDefinition Height="auto" />
|
<RowDefinition Height="auto" />
|
||||||
<RowDefinition Height="auto" />
|
<RowDefinition Height="auto" />
|
||||||
<RowDefinition Height="auto" />
|
<RowDefinition Height="auto" />
|
||||||
|
<RowDefinition Height="auto" />
|
||||||
|
<RowDefinition Height="auto" />
|
||||||
<RowDefinition Height="*" />
|
<RowDefinition Height="*" />
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<TextBlock Grid.Row="1" Margin="10 0" FontSize="28">
|
<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="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="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>
|
<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>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|||||||
@ -25,11 +25,14 @@ namespace SyncAsyncApp
|
|||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
private void executeSync_Click(object sender, RoutedEventArgs e)
|
||||||
private async void executeAsync_Click(object sender, RoutedEventArgs e)
|
|
||||||
{
|
{
|
||||||
|
resultsWindow.Text = "";
|
||||||
var watch = System.Diagnostics.Stopwatch.StartNew();
|
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||||
await RunDownloadASync();
|
|
||||||
|
var results = DemoMethods.RunDownloadSync();
|
||||||
|
PrintResults(results);
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
var elapsedMs = watch.ElapsedMilliseconds;
|
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();
|
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||||
|
|
||||||
RunDownloadSync();
|
var results = await DemoMethods.RunDownloadASync(progress);
|
||||||
|
PrintResults(results);
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
var elapsedMs = watch.ElapsedMilliseconds;
|
var elapsedMs = watch.ElapsedMilliseconds;
|
||||||
|
|
||||||
resultsWindow.Text += $"Total execution time: {elapsedMs}";
|
resultsWindow.Text += $"Total execution time: {elapsedMs}";
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RunDownloadASync()
|
private void ReportProgress(object sender, ProgressReportModel e)
|
||||||
{
|
{
|
||||||
|
dashboardProgress.Value = e.PercentageComplete;
|
||||||
List<string> websites = PrepData();
|
PrintResults(e.SitesDownoaded);
|
||||||
foreach (string site in websites)
|
|
||||||
{
|
|
||||||
WebsiteDataModel results = await Task.Run(()=> DownLoadWebSite(site));
|
|
||||||
ReportWebSiteInfo(results);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RunDownloadParallelASync()
|
|
||||||
{
|
|
||||||
|
|
||||||
List<string> websites = PrepData();
|
//private async Task RunDownloadParallelASync()
|
||||||
List<Task<WebsiteDataModel>> tasks = new List<Task<WebsiteDataModel>>();
|
//{
|
||||||
|
|
||||||
foreach (string site in websites)
|
// List<string> websites = PrepData();
|
||||||
{
|
// List<Task<WebsiteDataModel>> tasks = new List<Task<WebsiteDataModel>>();
|
||||||
// Make it async
|
|
||||||
// tasks.Add( Task.Run(() => DownLoadWebSite(site)));
|
|
||||||
|
|
||||||
//Use Native async
|
// foreach (string site in websites)
|
||||||
tasks.Add(DownLoadWebSiteAsync(site));
|
// {
|
||||||
}
|
// // 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)
|
// var results = await Task.WhenAll(tasks);
|
||||||
{
|
|
||||||
ReportWebSiteInfo(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
//private void ReportWebSiteInfo(WebsiteDataModel data)
|
||||||
{
|
//{
|
||||||
resultsWindow.Text += $"{data.WebsiteUrl} downloaded: {data.WebsiteData.Length} characters long. {Environment.NewLine}";
|
// 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)
|
private async void executeParallelAsync_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
resultsWindow.Text = "";
|
||||||
var watch = System.Diagnostics.Stopwatch.StartNew();
|
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||||
await RunDownloadParallelASync();
|
|
||||||
|
var results = await DemoMethods.RunDownloadParallelASync();
|
||||||
|
PrintResults(results);
|
||||||
|
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
var elapsedMs = watch.ElapsedMilliseconds;
|
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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
SyncAsyncApp/ProgressReportModel.cs
Normal file
14
SyncAsyncApp/ProgressReportModel.cs
Normal 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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -55,6 +55,8 @@
|
|||||||
<Generator>MSBuild:Compile</Generator>
|
<Generator>MSBuild:Compile</Generator>
|
||||||
<SubType>Designer</SubType>
|
<SubType>Designer</SubType>
|
||||||
</ApplicationDefinition>
|
</ApplicationDefinition>
|
||||||
|
<Compile Include="DemoMethods.cs" />
|
||||||
|
<Compile Include="ProgressReportModel.cs" />
|
||||||
<Compile Include="WebsiteDataModel.cs" />
|
<Compile Include="WebsiteDataModel.cs" />
|
||||||
<Page Include="MainWindow.xaml">
|
<Page Include="MainWindow.xaml">
|
||||||
<Generator>MSBuild:Compile</Generator>
|
<Generator>MSBuild:Compile</Generator>
|
||||||
|
|||||||
Reference in New Issue
Block a user