Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Diagnostics;
  16. using System.Threading;
  17. using System.ComponentModel;
  18.  
  19. namespace ThreadingClass
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window, INotifyPropertyChanged
  25. {
  26. public event PropertyChangedEventHandler PropertyChanged;
  27. private readonly string _data;
  28. private readonly string _searchTerm = "abc";
  29. private readonly SynchronizationContext _ctx = SynchronizationContext.Current;
  30.  
  31. private readonly long[] _processingTime = new long[1];
  32. private int _result = 0;
  33. private int _level = Environment.ProcessorCount;
  34.  
  35. public int ConcurrencyLevel
  36. {
  37. get { return _level; }
  38. set
  39. {
  40. _level = value;
  41. if (null != PropertyChanged)
  42. PropertyChanged(this, new PropertyChangedEventArgs("ConcurrencyLevel"));
  43. }
  44. }
  45.  
  46.  
  47. public MainWindow()
  48. {
  49. InitializeComponent();
  50. DataContext = this;
  51. _data = DataHelper.LoadData();
  52. }
  53.  
  54. private void ResetTimeAndResult(object sender)
  55. {
  56. ((Button)sender).IsEnabled = false;
  57. ((Button)sender).Content = "running....";
  58. _processingTime[0] = 0;
  59. _result = 0;
  60. }
  61.  
  62. private void WriteResult(Label target, object sender)
  63. {
  64. _ctx.Post(_ =>
  65. {
  66. ((Button)sender).IsEnabled = true;
  67. ((Button)sender).Content = "Run";
  68.  
  69. target.Content = string.Format("Found {0} occurences of '{1}' in {2} ms", _result, _searchTerm, _processingTime[0]);
  70. }, null);
  71. }
  72.  
  73. private void btnSingleThread_Click(object sender, RoutedEventArgs e)
  74. {
  75. ResetTimeAndResult(sender);
  76.  
  77. foreach (var func in DataHelper.FindStringCountActions(_data, _searchTerm, _processingTime, ConcurrencyLevel))
  78. {
  79. _result += func();
  80. }
  81. WriteResult(Res1, sender);
  82. }
  83.  
  84. private void btnNewThread_Click(object sender, RoutedEventArgs e)
  85. {
  86. ResetTimeAndResult(sender);
  87. var functions = DataHelper.FindStringCountActions(_data, _searchTerm, _processingTime, ConcurrencyLevel);
  88. int scheduled = functions.Count;
  89.  
  90. foreach (var func in functions)
  91. {
  92. ThreadStart function = () =>
  93. {
  94. int foundStrings = func();
  95.  
  96. Interlocked.Add(ref _result, foundStrings);
  97.  
  98. if (Interlocked.Decrement(ref scheduled) == 0)
  99. WriteResult(Res2, sender);
  100. };
  101.  
  102. Thread th = new Thread(function);
  103. th.Start();
  104. }
  105.  
  106. }
  107.  
  108. private void btnThredPool_Click(object sender, RoutedEventArgs e)
  109. {
  110. ResetTimeAndResult(sender);
  111. var functions = DataHelper.FindStringCountActions(_data, _searchTerm, _processingTime, ConcurrencyLevel);
  112. int scheduled = functions.Count;
  113.  
  114. foreach (var func in functions)
  115. {
  116. WaitCallback function = _ =>
  117. {
  118. int foundStrings = func();
  119.  
  120. Interlocked.Add(ref _result, foundStrings);
  121.  
  122. if (Interlocked.Decrement(ref scheduled) == 0)
  123. WriteResult(Res3, sender);
  124. };
  125.  
  126. ThreadPool.QueueUserWorkItem(function, null);
  127. }
  128. }
  129.  
  130. private void btnTask_Click(object sender, RoutedEventArgs e)
  131. {
  132. ResetTimeAndResult(sender);
  133. var functions = DataHelper.FindStringCountActions(_data, _searchTerm, _processingTime, ConcurrencyLevel);
  134. int scheduled = functions.Count;
  135.  
  136. List<Task<int>> tasks = new List<Task<int>>();
  137.  
  138. foreach (Func<int> function in functions)
  139. {
  140. //do zad dom. function.BeginInvoke(
  141. Task<int> task = Task.Factory.StartNew<int>(function);
  142. tasks.Add(task);
  143. }
  144.  
  145. Task.WhenAll<int>(tasks)
  146. .ContinueWith(partialTasks => _result = partialTasks.Result.Sum())
  147. .ContinueWith(result => WriteResult(Res4, sender));
  148. }
  149.  
  150. private void btnClassicApm_Click(object sender, RoutedEventArgs e)
  151. {
  152. // zad dom :D
  153. }
  154.  
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement