Last active
April 22, 2021 20:24
-
-
Save urza/4bcc9002fbc7bfbeb6f8837bc821f668 to your computer and use it in GitHub Desktop.
ParallelWPFProgress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void btnWordStatsParallelInTaskWithProgress_Click(object sender, RoutedEventArgs e) | |
{ | |
txbResultsInfo.Text = ""; | |
IProgress<string> progress = new Progress<string>(message => | |
{ | |
txbResultsInfo.Text += message; | |
}); | |
await Task.Run(() => Parallel.ForEach(_files, (file) => | |
{ | |
Dictionary<string, int> stats = new Dictionary<string, int>(); | |
foreach (var word in File.ReadAllLines(file.FullName)) | |
{ | |
if (stats.ContainsKey(word)) | |
stats[word]++; | |
else | |
stats.Add(word, 1); | |
} | |
progress.Report(file.Name + Environment.NewLine + | |
string.Join(Environment.NewLine, stats.OrderByDescending(x => x.Value).Take(10) | |
.Select(x => x.Key + ": " + x.Value)) | |
+ Environment.NewLine + Environment.NewLine); | |
})); | |
txbResultsInfo.Text += "Elapsed ms: " + w.ElapsedMilliseconds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment