Last active
April 22, 2021 19:56
-
-
Save urza/cecc92371df11a1f3e8f189f676e12f4 to your computer and use it in GitHub Desktop.
Paralallel continuation
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 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