システムを使用する;
System.Collections.Generic を使用します。
System.ComponentModel を使用します。
System.Data を使用します。
System.Drawing を使用します。
System.Linq を使用します。
System.Text を使用します。
System.Threading.Tasks を使用します。
System.Windows.Forms を使用します。
System.Threading を使用します。
名前空間 bingfazhixingtest
{ パブリック部分クラス Form1 : Form {
public ParameterizedThreadStart process1;
public ParameterizedThreadStart process2;
パブリックスレッド thread1;
パブリックスレッド thread2;
パブリックブール bThread1;
パブリックブール bThread2;
public Form1()
{ InitializeComponent();
//创建線程
process1 = new ParameterizedThreadStart(Add1);
process2 = 新しい ParameterizedThreadStart(Add2);
スレッド1 = 新しいスレッド(プロセス1);
thread2 = 新しいスレッド(プロセス2);
}
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) { // 2 つの変数を作成し、ループを停止します
bThread2 = true;
thread2.Start();
}
private void button3_Click(オブジェクト送信者, EventArgs e)
{ bThread1 = false; }
private void button4_Click(object sender, EventArgs e)
{ //bThread2 = false; thread2.Abort(); //スレッドの破棄 } } }
スレッドのその他の操作
thread2.Suspend();//スレッドを一時停止
thread2.Abort();//スレッドを中止
thread2.Resume();//スレッドを続行