Form1.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace _08_2_Threads
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. volatile bool STOP = false;
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. button2.Focus();
  23. STOP = false;
  24. listBox1.Items.Clear();
  25. int n = 100;
  26. for (int i = 0; i < n; i++)
  27. {
  28. listBox1.Items.Add($"{i}");
  29. Thread.Sleep(100);
  30. Application.DoEvents();
  31. progressBar1.Value = 100 * i / n;
  32. if (STOP)
  33. break;
  34. }
  35. }
  36. private void button2_Click(object sender, EventArgs e)
  37. {
  38. STOP = true;
  39. }
  40. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  41. {
  42. progressBar1.Value = e.ProgressPercentage;
  43. label1.Text = $"Jest godz {DateTime.Now:HH:mm:ss} wykonano {e.ProgressPercentage} % operacji";
  44. }
  45. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  46. {
  47. int n = (int)e.Argument;
  48. for (int i = 0; i < n; i++)
  49. {
  50. //if (i == 50)
  51. // throw new Exception("Symulujemy błąd!");
  52. listBox1.Invoke(new Action(() => { listBox1.Items.Add($"{i}"); }));
  53. Thread.Sleep(100);
  54. backgroundWorker1.ReportProgress((i + 1) * 100 / n);
  55. if (backgroundWorker1.CancellationPending)
  56. {
  57. e.Cancel = true;
  58. break;
  59. }
  60. }
  61. e.Result = new string[] { "Koniec operacji OK", "129847293685.123" };
  62. }
  63. private void button3_Click(object sender, EventArgs e)
  64. {
  65. STOP = false;
  66. listBox1.Items.Clear();
  67. backgroundWorker1.RunWorkerAsync(120);
  68. }
  69. private void button4_Click(object sender, EventArgs e)
  70. {
  71. backgroundWorker1.CancelAsync();
  72. }
  73. private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  74. {
  75. if (e.Error != null)
  76. MessageBox.Show(e.Error.Message);
  77. else
  78. {
  79. if (e.Cancelled)
  80. MessageBox.Show("User anulował operację!");
  81. else
  82. {
  83. if (e.Result != null)
  84. {
  85. string[] res = e.Result as string[];
  86. MessageBox.Show($"Operacja zakończona\r\n{res[0]}\r\n{res[1]}");
  87. }
  88. else
  89. MessageBox.Show($"Operacja zakończona");
  90. }
  91. }
  92. }
  93. Thread thread;
  94. private void button6_Click(object sender, EventArgs e)
  95. {
  96. listBox1.Items.Clear();
  97. thread = new Thread(() =>
  98. {
  99. int n = 100;
  100. for (int i = 0; i < n; i++)
  101. {
  102. listBox1.Invoke(new Action(() =>
  103. {
  104. listBox1.Items.Add($"{i}");
  105. progressBar1.Value = (i + 1) * 100 / n;
  106. }));
  107. Thread.Sleep(100);
  108. }
  109. });
  110. thread.Start();
  111. }
  112. private void button5_Click(object sender, EventArgs e)
  113. {
  114. thread.Suspend();
  115. }
  116. private void button7_Click(object sender, EventArgs e)
  117. {
  118. thread.Resume();
  119. }
  120. private void button8_Click(object sender, EventArgs e)
  121. {
  122. if (thread != null && thread.ThreadState == ThreadState.Running)
  123. {
  124. thread.Abort();
  125. }
  126. }
  127. void robota(object o)
  128. {
  129. int n = (int)o;
  130. for (int i = 0; i < n; i++)
  131. {
  132. listBox1.Invoke(new Action(() =>
  133. {
  134. listBox1.Items.Add($"{i}");
  135. progressBar1.Value = (i + 1) * 100 / n;
  136. }));
  137. Thread.Sleep(100);
  138. }
  139. }
  140. private void button9_Click(object sender, EventArgs e)
  141. {
  142. listBox1.Items.Clear();
  143. thread = new Thread(robota);
  144. thread.Start(90);
  145. }
  146. private void button10_Click(object sender, EventArgs e)
  147. {
  148. Task.Run(() =>
  149. {
  150. int n = 100;
  151. for (int i = 0; i < n; i++)
  152. {
  153. listBox1.Invoke(new Action(() =>
  154. {
  155. listBox1.Items.Add($"{i}");
  156. progressBar1.Value = (i + 1) * 100 / n;
  157. }));
  158. Thread.Sleep(100);
  159. }
  160. });
  161. }
  162. }
  163. }