123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace _08_2_Threads
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- volatile bool STOP = false;
- private void button1_Click(object sender, EventArgs e)
- {
- button2.Focus();
- STOP = false;
- listBox1.Items.Clear();
- int n = 100;
- for (int i = 0; i < n; i++)
- {
- listBox1.Items.Add($"{i}");
- Thread.Sleep(100);
- Application.DoEvents();
- progressBar1.Value = 100 * i / n;
- if (STOP)
- break;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- STOP = true;
- }
- private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- progressBar1.Value = e.ProgressPercentage;
- label1.Text = $"Jest godz {DateTime.Now:HH:mm:ss} wykonano {e.ProgressPercentage} % operacji";
- }
- private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
- {
- int n = (int)e.Argument;
- for (int i = 0; i < n; i++)
- {
- //if (i == 50)
- // throw new Exception("Symulujemy błąd!");
- listBox1.Invoke(new Action(() => { listBox1.Items.Add($"{i}"); }));
- Thread.Sleep(100);
- backgroundWorker1.ReportProgress((i + 1) * 100 / n);
- if (backgroundWorker1.CancellationPending)
- {
- e.Cancel = true;
- break;
- }
- }
- e.Result = new string[] { "Koniec operacji OK", "129847293685.123" };
- }
- private void button3_Click(object sender, EventArgs e)
- {
- STOP = false;
- listBox1.Items.Clear();
- backgroundWorker1.RunWorkerAsync(120);
- }
- private void button4_Click(object sender, EventArgs e)
- {
- backgroundWorker1.CancelAsync();
- }
- private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- if (e.Error != null)
- MessageBox.Show(e.Error.Message);
- else
- {
- if (e.Cancelled)
- MessageBox.Show("User anulował operację!");
- else
- {
- if (e.Result != null)
- {
- string[] res = e.Result as string[];
- MessageBox.Show($"Operacja zakończona\r\n{res[0]}\r\n{res[1]}");
- }
- else
- MessageBox.Show($"Operacja zakończona");
- }
- }
- }
- Thread thread;
- private void button6_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- thread = new Thread(() =>
- {
- int n = 100;
- for (int i = 0; i < n; i++)
- {
- listBox1.Invoke(new Action(() =>
- {
- listBox1.Items.Add($"{i}");
- progressBar1.Value = (i + 1) * 100 / n;
- }));
- Thread.Sleep(100);
- }
- });
- thread.Start();
- }
- private void button5_Click(object sender, EventArgs e)
- {
- thread.Suspend();
- }
- private void button7_Click(object sender, EventArgs e)
- {
- thread.Resume();
- }
- private void button8_Click(object sender, EventArgs e)
- {
- if (thread != null && thread.ThreadState == ThreadState.Running)
- {
- thread.Abort();
- }
- }
- void robota(object o)
- {
- int n = (int)o;
- for (int i = 0; i < n; i++)
- {
- listBox1.Invoke(new Action(() =>
- {
- listBox1.Items.Add($"{i}");
- progressBar1.Value = (i + 1) * 100 / n;
- }));
- Thread.Sleep(100);
- }
- }
- private void button9_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- thread = new Thread(robota);
- thread.Start(90);
- }
- private void button10_Click(object sender, EventArgs e)
- {
- Task.Run(() =>
- {
- int n = 100;
- for (int i = 0; i < n; i++)
- {
- listBox1.Invoke(new Action(() =>
- {
- listBox1.Items.Add($"{i}");
- progressBar1.Value = (i + 1) * 100 / n;
- }));
- Thread.Sleep(100);
- }
- });
- }
- }
- }
|