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 bingfazhixingtest
{
public partial class Form1 : Form
{
public ParameterizedThreadStart process1;
public ParameterizedThreadStart process2;
public Thread thread1;
public Thread thread2;
public bool bThread1;
public bool bThread2;
public Form1()
{
InitializeComponent();
//创建线程
process1 = new ParameterizedThreadStart(Add1);
process2 = new ParameterizedThreadStart(Add2);
thread1 = new Thread(process1);
thread2 = new Thread(process2);
}
private void Add1(object obj)
{
while (true)
{
for (int a = 10; a < 100000; a = a + 2)
{
if (bThread1) {
this.Invoke(new Action(() => label1.Text = a.ToString()));
Thread.Sleep(200);
}
//label2.Text = a.ToString();
}
Console.ReadLine();
}
}
private void Add2(object obj)
{
while (true)
{
for (int a = 10; a < 100000; a = a + 1)
{
if (bThread2)
{
this.Invoke(new Action(() => label2.Text = a.ToString()));
Thread.Sleep(200);
// label2.Text = a.ToString();
}
}
Console.ReadLine();
}
}
private void button1_Click(object sender, EventArgs e)
{
//ThreadPool.QueueUserWorkItem(state => CountTo(int.Parse(textBox1.Text), cts.Token));
bThread1 = true;
thread1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
//创建两个变量,停止循环
bThread2 = true;
thread2.Start();
}
private void button3_Click(object sender, EventArgs e)
{
bThread1 = false;
}
private void button4_Click(object sender, EventArgs e)
{
//bThread2 = false;
thread2.Abort(); //线程销毁
}
}
}
线程的其他操作
thread2.Suspend();//挂起线程
thread2.Abort();//中止线程
thread2.Resume();//继续线程