namespace WindowsFormsApp2
{
public partial class Form2 : Form
{
Thread t = null;
public Form2()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
t.Abort();
}
private void button1_Click(object sender, EventArgs e)
{
t.Start(this.textBox1.Text);//启动
}
private void Form2_Load(object sender, EventArgs e)
{
// t = new Thread(new ThreadStart(Run));
t = new Thread(new ParameterizedThreadStart(Run));
}
private void Run(object name)
{
for (int i = 1; i < 20000; i++)//循环条数
{
this.Invoke(new Action(
() => {
this.richTextBox1.Text = name+"第" + i + "条记录完成";
this.progressBar1.Value = i / 200; //进度框
this.label2.Text = i / 200+"%";//显示百分比
}
));
Thread.Sleep(10);
}
}
private void button2_Click(object sender, EventArgs e)
{
Thread.Sleep(5000);//主线程睡眠5秒
}
private void button3_Click(object sender, EventArgs e)
{
if (t.ThreadState != ThreadState.Suspended)//挂起
{
t.Suspend();
}
}
private void button4_Click(object sender, EventArgs e)
{
if (t.ThreadState == ThreadState.Suspended)//等于挂起
{
t.Resume();//恢复
}
}
}
}
Windows 窗体 线程
最新推荐文章于 2020-04-10 09:23:32 发布