Task和Thread启动、暂停、继续、退出操作
演示效果如下:
task的操作源代码如下:
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class Form1 : Form
{
CancellationTokenSource tokenSource;
ManualResetEvent resetEvent;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (tokenSource != null)
{
tokenSource.Cancel();
}
tokenSource = new CancellationTokenSource();
resetEvent = new ManualResetEvent(true);
CancellationToken token = tokenSource.Token;
Task task = new Task(() =>
{
int num = 0;
while (true)
{
if (token.IsCancellationRequested)
{
this.BeginInvoke((EventHandler)(delegate { label1.Text = "退出"; }));
break;
}
// 初始化为true时执行WaitOne不阻塞
resetEvent.WaitOne();
this.BeginInvoke((EventHandler)(delegate { label1.Text = (num++).ToString(); }));
Thread.Sleep(200);
}
}, token);
task.Start();
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
resetEvent.Reset();
}
/// <summary>
/// 继续
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
resetEvent.Set();
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
tokenSource.Cancel();
}
}
}
Thread的启动操作如下,同时添加了Thread线程的退出检测,以及Thread参数传递。
演示效果如下:
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class Form1 : Form
{
CancellationTokenSource tokenSource;
ManualResetEvent resetEvent;
public Form1()
{
InitializeComponent();
}
private void test(object obj)
{
var a = ((string, int))obj; // 传递多个参数的方式
if (tokenSource != null)
{
tokenSource.Cancel();
}
tokenSource = new CancellationTokenSource();
resetEvent = new ManualResetEvent(true);
CancellationToken token = tokenSource.Token;
int num = 0;
while (true)
{
if (token.IsCancellationRequested)
{
this.BeginInvoke((EventHandler)(delegate { label1.Text = "退出"; }));
break;
}
// 初始化为true时执行WaitOne不阻塞
resetEvent.WaitOne();
this.BeginInvoke((EventHandler)(delegate { label1.Text = (num++).ToString(); }));
Thread.Sleep(200);
}
}
private void StopSchedule(Thread t)
{
new Task(() =>
{
bool ret = true;
while (ret)
{
this.BeginInvoke((EventHandler)(delegate
{
ret = t.IsAlive;
label2.Text = t.IsAlive.ToString();
}));
Thread.Sleep(1000);
}
}).Start();
}
/// <summary>
/// 启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ParameterizedThreadStart(test));
t.Start(("OK", 123)); // 传递多个参数
StopSchedule(t); // 等待Thread结束
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
resetEvent.Reset();
}
/// <summary>
/// 继续
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
resetEvent.Set();
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
tokenSource.Cancel();
}
}
}