Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Windows.Forms;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- static AutoResetEvent waitHandler = new AutoResetEvent (true);
- static int res = 0;
- object syncObj = new object();
- // static Mutex mutex = new Mutex(false, "oreilly.com OneAtATimeDemo");
- private void button1_Click(object sender, EventArgs e)
- {
- int count_threads = 8;
- Thread a = new Thread(first),
- b = new Thread(second),
- c = new Thread(third),
- d = new Thread(fourth),
- ee = new Thread(fifth),
- f = new Thread(sixth),
- g = new Thread(seventh),
- h = new Thread(eight);
- // Упаковываем
- Data[] temp = new Data[count_threads];
- for (int i = 0; i != count_threads; ++i) {
- temp[i].a = 1;
- temp[i].b = 2;
- temp[i].ttt = waitHandler;
- }
- // Начинаем, и передаем данные
- a.Start(temp[0]);
- b.Start(temp[1]);
- c.Start(temp[2]);
- d.Start(temp[3]);
- ee.Start(temp[4]);
- f.Start(temp[5]);
- g.Start(temp[6]);
- h.Start(temp[7]);
- // thread.Join();
- waitHandler.WaitOne();
- }
- private void first(object input)
- {
- for (int i = 0; i != 15; ++i) {
- work();
- Invoke(new Action(() => textBox5.AppendText(Convert.ToString(res) + "\n")));
- //textBox5.AppendText(Convert.ToString(i));
- }
- Thread.Sleep(3000);
- for (int i = 0; i != 15; ++i)
- {
- work();
- Invoke(new Action(() => textBox5.AppendText(Convert.ToString(res) + "\n")));
- //textBox5.AppendText(Convert.ToString(i));
- }
- }
- private void second(object input)
- {
- Thread.Sleep(500);
- for (int i = 0; i != 15; ++i)
- {
- work();
- Invoke(new Action(() => textBox6.AppendText(Convert.ToString(res) + "\n")));
- //textBox5.AppendText(Convert.ToString(i));
- }
- }
- private void third(object input)
- {
- int id = Thread.CurrentThread.ManagedThreadId;
- Invoke(new Action(() => textBox3.Text = Convert.ToString(id)));
- }
- private void fourth(object input)
- {
- //Thread.Sleep(5000);
- Data data = (Data)input;
- int id = Thread.CurrentThread.ManagedThreadId;
- Invoke(new Action(() => textBox4.Text = Convert.ToString(id)));
- data.ttt.Set();
- }
- private void fifth(object input)
- {
- int id = Thread.CurrentThread.ManagedThreadId;
- Invoke(new Action(() => textBox3.Text = Convert.ToString(id)));
- }
- private void sixth(object input)
- {
- //Thread.Sleep(5000);
- Data data = (Data)input;
- int id = Thread.CurrentThread.ManagedThreadId;
- Invoke(new Action(() => textBox4.Text = Convert.ToString(id)));
- data.ttt.Set();
- }
- private void seventh(object input)
- {
- int id = Thread.CurrentThread.ManagedThreadId;
- Invoke(new Action(() => textBox3.Text = Convert.ToString(id)));
- }
- private void eight(object input)
- {
- //Thread.Sleep(5000);
- Data data = (Data)input;
- int id = Thread.CurrentThread.ManagedThreadId;
- Invoke(new Action(() => textBox4.Text = Convert.ToString(id)));
- data.ttt.Set();
- }
- private void work()
- {
- lock (syncObj) {
- ++res;
- }
- }
- }
- // Отдельный тип для упаковки/распаковки
- struct Data
- {
- public int a;
- public int b;
- public AutoResetEvent ttt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement