| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 738 人关注过本帖
标题:C#多线程的问题
只看楼主 加入收藏
Maxico
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-8-10
收藏
 问题点数:0 回复次数:2 
C#多线程的问题
C#多线程的问题:

编写段Windows 多线程程序,A线程输出1-100的奇数,B线程输出1-100的偶数,在AB线程结束后触发一个自定义ChangeColor(这个事件修改Windows窗体的背景色为红色)
搜索更多相关主题的帖子: 线程 定义 Windows 偶数 ChangeColor 
2005-08-14 15:03
Maxico
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-8-10
收藏
得分:0 

试验一: 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); } }

}

}

2005-08-17 11:04
Maxico
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-8-10
收藏
得分:0 

试验二: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading;

namespace _12 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Threading.Thread myThreadA; private System.Threading.Thread myThreadB; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.ListBox listBox2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button2; public static int q1=100; public static int q2=99; //public testInt tttt; private delegate void ChangeColor(); private event ChangeColor myEvent; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;

public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); myEvent +=new ChangeColor(Form1_myEvent); //tttt=new testInt();

// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // }

/// <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.button1 = new System.Windows.Forms.Button(); this.listBox1 = new System.Windows.Forms.ListBox(); this.listBox2 = new System.Windows.Forms.ListBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(8, 56); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(72, 24); this.button1.TabIndex = 2; this.button1.Text = "开始计算"; this.button1.Click += new System.EventHandler(this.button1_Click); // // listBox1 // this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(88, 16); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(256, 304); this.listBox1.TabIndex = 3; // // listBox2 // this.listBox2.ItemHeight = 12; this.listBox2.Location = new System.Drawing.Point(416, 16); this.listBox2.Name = "listBox2"; this.listBox2.Size = new System.Drawing.Size(248, 304); this.listBox2.TabIndex = 4; // // label1 // this.label1.Location = new System.Drawing.Point(32, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(56, 24); this.label1.TabIndex = 5; this.label1.Text = "奇数"; // // label2 // this.label2.Location = new System.Drawing.Point(360, 16); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(48, 16); this.label2.TabIndex = 6; this.label2.Text = "偶数"; // // button2 // this.button2.Location = new System.Drawing.Point(8, 136); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(72, 24); this.button2.TabIndex = 7; this.button2.Text = "退出"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(896, 341); this.Controls.Add(this.button2); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.listBox2); this.Controls.Add(this.listBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);

} #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> //[STAThread] public class ThreadTest { [STAThread] static void Main() { Application.Run(new Form1());} } private void button1_Click(object sender, System.EventArgs e) { this.myThreadA = new Thread(new System.Threading.ThreadStart(DoA)); this.myThreadB = new Thread(new System.Threading.ThreadStart(DoB)); myThreadA.Name="奇数线程"; myThreadB.Name="偶数线程"; myThreadA.Start(); myThreadB.Start(); //bool b=true; //bool b1=false; //bool b2=false; while(myThreadA.IsAlive && myThreadB.IsAlive) { Application.DoEvents(); Thread.Sleep(10); } this.Form1_myEvent();

//int aa=0; // Thread.Sleep(100); // while(!(myThreadA.IsAlive && myThreadB.IsAlive)); // { // myThreadA.Suspend(); // myThreadB.Suspend(); // this.Form1_myEvent(); // // }

}

public void DoA() { int i; for(i=1;i<101;i++) { if(i%2!=0) { /*lock(tttt) { tttt.q1=i; }*/

Console.WriteLine("Thread A={0}",i); this.listBox1.Items.Add(i.ToString()); Thread.Sleep(100); //int a=0; } } } public void DoB() { int i; for(i=1;i<101;i++) { if(i%2==0) { /*lock(tttt) { tttt.q2=i; }*/ Console.WriteLine("Thread B={0}",i); this.listBox2.Items.Add(i.ToString()); Thread.Sleep(100);

//int a=0; }

} }

private void Form1_myEvent() { this.BackColor = Color.Red; }

private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); }

} /*public class testInt { public testInt() { q1=0; q2=0; } public int q1,q2; }*/ }

2005-08-17 11:08
快速回复:C#多线程的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024950 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved