Refactored Listings 20 1&2 continued (#WIP)
MODIFY
src/Chapter20.Tests/Listing20.01.Tests.cs
src/Chapter20.Tests/Listing20.01.Tests.cs
@@ -14,9 +14,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_01.Tests
{
ProgramWrapper = new ProgramWrapper(
(args)=>
- Task.Run(() => Program.Main(args)),
- (findText, url, progress) =>
- Task.Run(()=>Program.FindTextInWebUri(findText, url.First())));
+ Task.Run(() => Program.Main(args)));
}
}
}

MODIFY
src/Chapter20/Listing20.01.SynchronousWebRequestHighLatency.cs
src/Chapter20/Listing20.01.SynchronousWebRequestHighLatency.cs
@@ -24,13 +24,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_01
}
Console.Write(url);
- Console.WriteLine("Searching...");
-
+ Console.WriteLine("Downloading...");
using WebClient webClient = new WebClient();
-
byte[] downloadData =
webClient.DownloadData(url);
+ Console.WriteLine("Searching...");
int textOccurrenceCount = CountOccurrences(
downloadData, findText);

MODIFY
src/Chapter20/Listing20.02.AsynchronousHighLatencyUsingTaskAsynchronyPattern.cs
src/Chapter20/Listing20.02.AsynchronousHighLatencyUsingTaskAsynchronyPattern.cs
@@ -7,7 +7,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_02
static public class Program
{
- async public static Task Main(string[] args)
+ public static async Task Main(string[] args)
{
if (args.Length == 0)
{
@@ -25,6 +25,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_02
}
Console.Write(url);
+#if IProgress
IProgress<DownloadProgressChangedEventArgs> progress =
new Progress<DownloadProgressChangedEventArgs>((value) =>
{
@@ -32,8 +33,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_02
}
);
- using WebClient webClient = new WebClient();
-
if (progress is object)
{
webClient.DownloadProgressChanged += (sender, eventArgs) =>
@@ -41,7 +40,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_02
progress.Report(eventArgs);
};
}
+#endif
+ using WebClient webClient = new WebClient();
Task<byte[]> taskDownload =
webClient.DownloadDataTaskAsync(url);
@@ -59,25 +60,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_02
Console.WriteLine(textOccurrenceCount);
}
- public async static Task<int> FindTextInWebUriAsync(
- string findText, string url,
- IProgress<DownloadProgressChangedEventArgs>? progressCallback = null)
- {
- using WebClient webClient = new WebClient();
- if (progressCallback is object)
- {
- webClient.DownloadProgressChanged += (sender, eventArgs) =>
- {
- progressCallback.Report(eventArgs);
- };
- }
-
- byte[] downloadData =
- await webClient.DownloadDataTaskAsync(url);
-
- return await CountOccurrencesAsync(
- downloadData, findText);
- }
private static async Task<int> CountOccurrencesAsync(
byte[] downloadData, string findText)

MODIFY
src/Chapter21/Listing21.10.CancelingParallelLoop.cs
src/Chapter21/Listing21.10.CancelingParallelLoop.cs
@@ -27,7 +27,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_10
}).ToList();
}
- public static void Main()
+ public static async void Main()
{
ConsoleColor originalColor = Console.ForegroundColor;
List<string> data = Utility.GetData(100000).ToList();
@@ -39,7 +39,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_10
// Use Task.Factory.StartNew<string>() for
// TPL prior to .NET 4.5
- Task task = Task.Run(() =>
+ async Task.Run(() =>
{
data = ParallelEncrypt(data, cts.Token);
}, cts.Token);

Refactored Listings 20 1&2 continued (#WIP)