Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11.  
  12.  
  13. namespace WindowsFormsApp1
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. static AutoResetEvent waitHandler = new AutoResetEvent (true);
  23. static int res = 0;
  24. object syncObj = new object();
  25. // static Mutex mutex = new Mutex(false, "oreilly.com OneAtATimeDemo");
  26.  
  27.  
  28. private void button1_Click(object sender, EventArgs e)
  29. {
  30. int count_threads = 8;
  31. Thread a = new Thread(first),
  32. b = new Thread(second),
  33. c = new Thread(third),
  34. d = new Thread(fourth),
  35. ee = new Thread(fifth),
  36. f = new Thread(sixth),
  37. g = new Thread(seventh),
  38. h = new Thread(eight);
  39.  
  40. // Упаковываем
  41. Data[] temp = new Data[count_threads];
  42. for (int i = 0; i != count_threads; ++i) {
  43. temp[i].a = 1;
  44. temp[i].b = 2;
  45. temp[i].ttt = waitHandler;
  46. }
  47.  
  48.  
  49.  
  50. // Начинаем, и передаем данные
  51. a.Start(temp[0]);
  52. b.Start(temp[1]);
  53. c.Start(temp[2]);
  54. d.Start(temp[3]);
  55. ee.Start(temp[4]);
  56. f.Start(temp[5]);
  57. g.Start(temp[6]);
  58. h.Start(temp[7]);
  59. // thread.Join();
  60.  
  61. waitHandler.WaitOne();
  62. }
  63.  
  64. private void first(object input)
  65. {
  66. for (int i = 0; i != 15; ++i) {
  67.  
  68. work();
  69. Invoke(new Action(() => textBox5.AppendText(Convert.ToString(res) + "\n")));
  70. //textBox5.AppendText(Convert.ToString(i));
  71.  
  72. }
  73. Thread.Sleep(3000);
  74. for (int i = 0; i != 15; ++i)
  75. {
  76.  
  77. work();
  78. Invoke(new Action(() => textBox5.AppendText(Convert.ToString(res) + "\n")));
  79. //textBox5.AppendText(Convert.ToString(i));
  80.  
  81. }
  82.  
  83. }
  84.  
  85. private void second(object input)
  86. {
  87. Thread.Sleep(500);
  88. for (int i = 0; i != 15; ++i)
  89. {
  90.  
  91. work();
  92. Invoke(new Action(() => textBox6.AppendText(Convert.ToString(res) + "\n")));
  93. //textBox5.AppendText(Convert.ToString(i));
  94.  
  95. }
  96.  
  97. }
  98.  
  99. private void third(object input)
  100. {
  101.  
  102. int id = Thread.CurrentThread.ManagedThreadId;
  103. Invoke(new Action(() => textBox3.Text = Convert.ToString(id)));
  104.  
  105.  
  106. }
  107.  
  108. private void fourth(object input)
  109. {
  110.  
  111. //Thread.Sleep(5000);
  112. Data data = (Data)input;
  113. int id = Thread.CurrentThread.ManagedThreadId;
  114. Invoke(new Action(() => textBox4.Text = Convert.ToString(id)));
  115.  
  116. data.ttt.Set();
  117.  
  118. }
  119. private void fifth(object input)
  120. {
  121.  
  122. int id = Thread.CurrentThread.ManagedThreadId;
  123. Invoke(new Action(() => textBox3.Text = Convert.ToString(id)));
  124.  
  125.  
  126. }
  127.  
  128. private void sixth(object input)
  129. {
  130.  
  131. //Thread.Sleep(5000);
  132. Data data = (Data)input;
  133. int id = Thread.CurrentThread.ManagedThreadId;
  134. Invoke(new Action(() => textBox4.Text = Convert.ToString(id)));
  135.  
  136. data.ttt.Set();
  137.  
  138. }
  139. private void seventh(object input)
  140. {
  141.  
  142. int id = Thread.CurrentThread.ManagedThreadId;
  143. Invoke(new Action(() => textBox3.Text = Convert.ToString(id)));
  144.  
  145.  
  146. }
  147.  
  148. private void eight(object input)
  149. {
  150.  
  151. //Thread.Sleep(5000);
  152. Data data = (Data)input;
  153. int id = Thread.CurrentThread.ManagedThreadId;
  154. Invoke(new Action(() => textBox4.Text = Convert.ToString(id)));
  155.  
  156. data.ttt.Set();
  157.  
  158. }
  159.  
  160. private void work()
  161. {
  162. lock (syncObj) {
  163. ++res;
  164. }
  165.  
  166. }
  167. }
  168.  
  169. // Отдельный тип для упаковки/распаковки
  170. struct Data
  171. {
  172. public int a;
  173. public int b;
  174. public AutoResetEvent ttt;
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement