using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace MulThread { public partial class Form1 : Form { public Form1() { InitializeComponent(); //TextBox.CheckForIllegalCrossThreadCalls = false;//显示计数不安全模式定义 dgShowMsgInTextBox = new DGShowMsgInTextBox(DoShowMsgInTextBox);//安全模式 } private void btnCount_Click(object sender, EventArgs e) { Count(); } private void Count() { DateTime begingTime = DateTime.Now; for (int i = 0; i < 999999999; i++) { txtNum.Invoke(dgShowMsgInTextBox, i.ToString()); } DateTime endTime = DateTime.Now; TimeSpan ts = endTime.Subtract(begingTime); MessageBox.Show("计算完毕,总耗时:"+ts.TotalMilliseconds.ToString()); } private void CountAndShow() { DateTime begingTime = DateTime.Now; for (int i = 0; i < 100000; i++) { txtNum.Text = i.ToString(); } DateTime endTime = DateTime.Now; TimeSpan ts = endTime.Subtract(begingTime); MessageBox.Show("计算完毕,总耗时:" + ts.TotalMilliseconds.ToString()); } private void CountAndShowSafety() { DateTime begingTime = DateTime.Now; for (int i = 0; i < 100000; i++) { this.Invoke(dgShowMsgInTextBox, i.ToString()); } DateTime endTime = DateTime.Now; TimeSpan ts = endTime.Subtract(begingTime); MessageBox.Show("计算完毕,总耗时:" + ts.TotalMilliseconds.ToString()); } //计算多线程 private void btnCountByThread_Click(object sender, EventArgs e) { Thread thread = new Thread(Count); thread.IsBackground = true; thread.Start(); } ////显示计数不安全模式 private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(CountAndShow); thread.IsBackground = true; thread.Start(); } //显示计数安全模式 private void button2_Click(object sender, EventArgs e) { Thread thread = new Thread(CountAndShowSafety); thread.IsBackground = true; thread.Start(); } delegate void DGShowMsgInTextBox(string msg); DGShowMsgInTextBox dgShowMsgInTextBox = null; void DoShowMsgInTextBox(string msg) { txtNum.Text = msg; } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace 线程执行带参数的方法 { public partial class Form1 : Form { public Form1() { InitializeComponent(); TextBox.CheckForIllegalCrossThreadCalls = false; } public void AddTest(object s) { txtNum.AppendText(s.ToString()); } public void AddTest2(object s) { myPara mp1 = s as myPara; txtNum.AppendText(mp1.a+mp1.b); } //计算第一个参数 private void btnCount_Click(object sender, EventArgs e) { ParameterizedThreadStart pts = new ParameterizedThreadStart(AddTest); Thread thread = new Thread(pts); thread.Start("hahahha"); } //计算多个参数 private void button1_Click(object sender, EventArgs e) { ParameterizedThreadStart pts = new ParameterizedThreadStart(AddTest2); Thread thread = new Thread(pts); myPara mp = new myPara(); mp.a = "aaaa"; mp.b = "bbbb"; thread.Start(mp); } //不安全访问结果 private void button2_Click(object sender, EventArgs e) { Thread thread1 = new Thread(SetTextStatic); thread1.Name = "t1"; Thread thread2 = new Thread(SetTextStatic); thread2.Name = "t2"; thread1.Start(); thread2.Start(); } void SetTextStatic() { for (int i = 0; i < 1000; i++) { int sco = int.Parse(txtNum.Text.Trim()); sco++; txtNum.Text = sco.ToString(); } } } } |