using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace TreadEx { class Class1 { public volatile bool shouldstop; private Form1 form1; public Class1(Form1 form1) { this.form1 = form1; } public void Method1(object obj) { string s = obj as string; form1.AddMessage(s); while (shouldstop==false) { Thread.Sleep(100); form1.AddMessage("a"); } form1.AddMessage("\n 线程Method1已终止"); } public void Method2() { while (shouldstop == false) { Thread.Sleep(100); form1.AddMessage("b"); } form1.AddMessage("\n 线程Method2已终止"); } } }
订阅了两次,执行两次,故产生了4个线程。委托button1_Click订阅事件button1.Click,就执行1次button1_click
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace TreadEx { public partial class Form1 : Form { Thread thread1, thread2; Class1 class1; public Form1() { InitializeComponent(); class1 = new Class1(this); button1.Click+=new EventHandler(button1_Click);//委托button1_Click订阅事件button1.Click,即执行1次
button2.Click+=new EventHandler(button2_Click); } private void button1_Click(object sender, EventArgs e) { richTextBox1.Clear(); class1.shouldstop = false; thread1 = new Thread(class1.Method1); thread1.IsBackground = true; thread2 = new Thread(class1.Method2); thread2.IsBackground = true; thread1.Start("A thread start \n"); thread2.Start(); } private void button2_Click(object sender, EventArgs e) { //class1.shouldstop = true; thread1.Abort(); //thread1.Join(); thread2.Abort(); //thread2.Join(); } private delegate void AddMessageDelegate(string message); public void AddMessage(string message) { if (richTextBox1.InvokeRequired) { AddMessageDelegate d = AddMessage; richTextBox1.Invoke(d, message); } else { richTextBox1.AppendText(message); } } }}button1.Click+=new EventHandler(button1_Click);//订阅了两次,执行两次,故产生了4个线程
由于thread1,thread2指向后生成的两个线程,所以通过thread1,thread2只能控制后两个线程,前生成的两个线程仍然运行。
结果:点击button2前输出aabb,点击button2后会继续输出a和b。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace TreadEx { public partial class Form1 : Form { Thread thread1, thread2; Class1 class1; public Form1() { InitializeComponent(); class1 = new Class1(this); button1.Click+=new EventHandler(button1_Click); button2.Click+=new EventHandler(button2_Click); } private void button1_Click(object sender, EventArgs e) { richTextBox1.Clear(); class1.shouldstop = false; thread1 = new Thread(class1.Method1); thread1.IsBackground = true; thread2 = new Thread(class1.Method2); thread2.IsBackground = true; thread1.Start("A thread start \n"); thread2.Start(); } private void button2_Click(object sender, EventArgs e) { class1.shouldstop = true; //thread1.Abort(); //thread1.Join(); //thread2.Abort(); //thread2.Join(); } private delegate void AddMessageDelegate(string message); public void AddMessage(string message) { if (richTextBox1.InvokeRequired) { AddMessageDelegate d = AddMessage; richTextBox1.Invoke(d, message); } else { richTextBox1.AppendText(message); } } } }结果:点击Button2后,四个线程全部结束。说明,使用volatile控制线程,比thread.abort更安全。
C# 线程练习1
最新推荐文章于 2022-10-04 20:43:12 发布