Последовательное выполнение потоков - C# (188819)
Формулировка задачи:
Здравствуйте. Есть два потока. Каждый меняет значение определенного ProgressBar`a. Как сделать так, чтоб второй ProgressBar начал заполнятся только после того, как первый будет заполнен. (Если возможно, это нужно сделать с помощью Mutex)
Данный код выполняется паралельно. Что я делаю неправильно?
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.Windows.Forms;
using System.Threading;
namespace Laba5_OS_
{
public partial class Form1 : Form
{
Mutex mutex = new Mutex();
public Form1()
{
InitializeComponent();
}
private void func1()
{
mutex.WaitOne();
for (int i = 0; i < 100; i++)
{
progressBar1.Invoke(new Action(() => progressBar1.Value = progressBar1.Value + 1));
}
mutex.ReleaseMutex();
}
private void func2()
{
// mutex.WaitOne();
for (int i = 0; i < 100; i++)
{
progressBar2.Invoke(new Action(() => progressBar2.Value = progressBar2.Value + 1));
}
// mutex.ReleaseMutex();
}
private void button1_Click(object sender, EventArgs e)
{
Thread myThread1 = new Thread(func1);
Thread myThread2 = new Thread(func2);
myThread1.Start();
myThread2.Start();
}
}
}Решение задачи: «Последовательное выполнение потоков»
textual
Листинг программы
public partial class Form1 : Form
{
Mutex mutex = new Mutex();
public Form1()
{
InitializeComponent();
}
private void func1()
{
mutex.WaitOne();
for (int i = 0; i < 100; i++)
{
progressBar1.Invoke(new Action(() => progressBar1.Value++));
Thread.Sleep(10);
}
mutex.ReleaseMutex();
}
private void func2()
{
mutex.WaitOne();
for (int i = 0; i < 100; i++)
{
progressBar2.Invoke(new Action(() => progressBar2.Value++));
Thread.Sleep(10);
}
mutex.ReleaseMutex();
}
private void button1_Click(object sender, EventArgs e)
{
Thread myThread1 = new Thread(func1);
Thread myThread2 = new Thread(func2);
myThread1.Start();
myThread2.Start();
}
}