Skip to content

Instantly share code, notes, and snippets.

@urza
Last active April 22, 2021 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urza/4bcc9002fbc7bfbeb6f8837bc821f668 to your computer and use it in GitHub Desktop.
Save urza/4bcc9002fbc7bfbeb6f8837bc821f668 to your computer and use it in GitHub Desktop.
ParallelWPFProgress
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