Skip to content

Instantly share code, notes, and snippets.

@urza
Last active April 22, 2021 19:56
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/cecc92371df11a1f3e8f189f676e12f4 to your computer and use it in GitHub Desktop.
Save urza/cecc92371df11a1f3e8f189f676e12f4 to your computer and use it in GitHub Desktop.
Paralallel continuation
private void btnWordStatsParallelInTask_Click(object sender, RoutedEventArgs e)
{
/*
10 nejcastejsich slov v kazdem souboru - paralelně
*/
txbResultsInfo.Text = "";
Stopwatch w = new Stopwatch();
w.Start();
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
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);
}
Dispatcher.Invoke(new Action(() =>
{
txbResultsInfo.Text += file.Name + Environment.NewLine;
//txbResultsInfo.Text += Environment.NewLine;
txbResultsInfo.Text += string.Join(Environment.NewLine, stats.OrderByDescending(x => x.Value).Take(10)
.Select(x => x.Key + ": " + x.Value));
txbResultsInfo.Text += Environment.NewLine + Environment.NewLine;
}), priority: System.Windows.Threading.DispatcherPriority.Send);
}))
.ContinueWith((task) =>
{
w.Stop();
txbResultsInfo.Text += "Elapsed ms: " + w.ElapsedMilliseconds;
}, TaskScheduler.FromCurrentSynchronizationContext());
Mouse.OverrideCursor = null;
//Dispatcher.BeginInvoke(new Action(() => txbResultsInfo.Text += "Elapsed ms: " + w.ElapsedMilliseconds));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment