C# Task 多线程 开始 暂停 停止
public partial class Form1 : Form
{
private ManualResetEvent ma;
private CancellationTokenSource tokenSource;
private CancellationToken token;
static readonly object objLock = new object();
private bool IsStart = false;
private bool IsManualResetEvent = true;
public Form1()
{
ma = new ManualResetEvent(IsManualResetEvent);
tokenSource = new CancellationTokenSource();
token = tokenSource.Token;
InitializeComponent();
}
/// <summary>
/// 开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (IsStart)
{
MessageBox.Show("程序已经启动!");
return;
}
else
{
tokenSource = new CancellationTokenSource();
token = tokenSource.Token;
IsStart = true;
Task.Run(() => Start01(), tokenSource.Token);
Task.Run(() => Start02(), tokenSource.Token);
Task.Run(() => Start03(), tokenSource.Token);
Task.Run(() => Start04(), tokenSource.Token);
}
}
public void Start01()
{
for (int i = 0; i < 9999; i++)
{
if (token.IsCancellationRequested)
{
return;
}
string str = "计数器:" + i;
Invoke(new Action(() => { this.richTextBox1.Text = str; }));
ma.WaitOne();
Thread.Sleep(100);
}
}
public void Start02()
{
for (int i = 0; i < 9999; i++)
{
if (token.IsCancellationRequested)
{
return;
}
string str = "计数器:" + i;
Invoke(new Action(() => { this.richTextBox2.Text = str; }));
ma.WaitOne();
Thread.Sleep(100);
}
}
public void Start03()
{
for (int i = 0; i < 9999; i++)
{
if (token.IsCancellationRequested)
{
return;
}
string str = "计数器:" + i;
Invoke(new Action(() => { this.richTextBox3.Text = str; }));
ma.WaitOne();
Thread.Sleep(100);
}
}
public void Start04()
{
for (int i = 0; i < 9999; i++)
{
if (token.IsCancellationRequested)
{
return;
}
string str = "计数器:" + i;
Invoke(new Action(() => { this.richTextBox4.Text = str; }));
ma.WaitOne();
Thread.Sleep(100);
}
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (IsStart)
{
ma.Reset();
}
}
/// <summary>
/// 继续
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (IsStart)
{
ma.Set();
}
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//线程取消 + 加上取消回调
tokenSource.Cancel();
tokenSource.Token.Register(() =>
{
IsStart = false;
});
}
/// <summary>
/// 窗体关闭事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 弹出提示框
DialogResult result = MessageBox.Show("确定要关闭窗体吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
// 关闭窗体
e.Cancel = false;
}
else
{
// 不关闭窗体
e.Cancel = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 计数清零
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
if (IsStart)
{
MessageBox.Show("程序已经启动!");
return;
}
this.richTextBox1.Text = "计数器:0";
this.richTextBox2.Text = "计数器:0";
this.richTextBox3.Text = "计数器:0";
this.richTextBox4.Text = "计数器:0";
}
}