编写段Windows 多线程程序,A线程输出1-100的奇数,B线程输出1-100的偶数,在AB线程结束后触发一个自定义ChangeColor(这个事件修改Windows窗体的背景色为红色)
试验一: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading;
namespace MultiThreads { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button btnOK; private System.Windows.Forms.RichTextBox richTextBox2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public delegate void DgEv (object sender,EventArgs e); public static event DgEv ChangeColor=null;
public delegate void DgCount(); public static event DgCount dgCount1=null; public static event DgCount dgCount2=null;
public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // ChangeColor+=new DgEv(Form1_ChangeColor); dgCount1+=new DgCount(Form1_dgCount1); dgCount2+=new DgCount(Form1_dgCount2); }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.btnOK = new System.Windows.Forms.Button(); this.richTextBox2 = new System.Windows.Forms.RichTextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(24, 32); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(152, 232); this.richTextBox1.TabIndex = 0; this.richTextBox1.Text = ""; // // btnOK // this.btnOK.Location = new System.Drawing.Point(160, 280); this.btnOK.Name = "btnOK"; this.btnOK.Size = new System.Drawing.Size(75, 32); this.btnOK.TabIndex = 1; this.btnOK.Text = "开始"; this.btnOK.Click += new System.EventHandler(this.btnOK_Click); // // richTextBox2 // this.richTextBox2.Location = new System.Drawing.Point(216, 32); this.richTextBox2.Name = "richTextBox2"; this.richTextBox2.Size = new System.Drawing.Size(160, 232); this.richTextBox2.TabIndex = 2; this.richTextBox2.Text = ""; // // label1 // this.label1.Location = new System.Drawing.Point(16, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(56, 16); this.label1.TabIndex = 3; this.label1.Text = "线程A"; // // label2 // this.label2.Location = new System.Drawing.Point(224, 8); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(48, 16); this.label2.TabIndex = 4; this.label2.Text = "线程B"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(400, 325); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.richTextBox2); this.Controls.Add(this.btnOK); this.Controls.Add(this.richTextBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
private void btnOK_Click(object sender, System.EventArgs e) {
ThreadStart worker1=new ThreadStart(WorkerThread1); ThreadStart worker2=new ThreadStart(WorkerThread2);
Thread t1=new Thread(worker1); Thread t2=new Thread(worker2); t1.Start(); t2.Start();
if((t1.IsAlive==false) && (t2.IsAlive==false)) { ChangeColor(this,null); }
}
private void Form1_ChangeColor(object sender, EventArgs e) { this.BackColor=Color.Red ; }
private void Form1_dgCount1() { richTextBox1.Text =str1; }
private void Form1_dgCount2() { richTextBox2.Text =str2; }
public static string str1; public static string str2;
public static void WorkerThread1() { for(int i=1;i<=100;i+=2) { str1=str1+i.ToString()+" "; Form1.dgCount1(); System.Threading.Thread.Sleep(10); } }
public static void WorkerThread2() { for(int j=2;j<=100;j+=2) { str2=str2+j.ToString()+" "; Form1.dgCount2(); System.Threading.Thread.Sleep(10); } }
}
}