您的位置:首页 > 编程语言 > C#

c# 多线程传递参数Demo (一个参数传递)

2010-02-05 14:10 525 查看



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 XianChen


{


public partial class Form3 : Form


{


public Form3()


{


InitializeComponent();


}




Thread t1, t2;


private void button1_Click(object sender, EventArgs e)


{


int i = 100;


t1 = new Thread(this.th1);


t1.Start(i);


}




private void button2_Click(object sender, EventArgs e)


{


t1.Suspend();


}




private void button7_Click(object sender, EventArgs e)


{


t1.Resume();


}




private void button3_Click(object sender, EventArgs e)


{


t1.Abort();


}




private void button4_Click(object sender, EventArgs e)


{


int i = 200;


t2 = new Thread(this.th2);


t2.Start(i);


}




private void button5_Click(object sender, EventArgs e)


{


t2.Suspend();


}




private void button8_Click(object sender, EventArgs e)


{


t2.Resume();


}




private void button6_Click(object sender, EventArgs e)


{


t2.Abort();


}






private delegate void weituo(object tmp);


private void th1(object tmp)


{


while (true)


{


if (this.InvokeRequired)


{


this.Invoke(new weituo(js1),tmp);


Thread.Sleep(1000);


}


else


{


js1(tmp);


}


}




}




private void th2(object tmp)


{


while (true)


{


if (this.InvokeRequired)


{


this.Invoke(new weituo(js2),tmp);


Thread.Sleep(1000);


}


else


{


js2(tmp);


}


}




}




private void js1(object tmp)


{


listBox1.Items.Add("这里是线程 1 在传递参数:"+tmp);


}




private void js2(object tmp)


{


listBox1.Items.Add("这里是线程 2 在传递参数:" + tmp);


}




}


}



本文出自 “知识就是财富” 博客,请务必保留此出处http://lovefly.blog.51cto.com/914912/274625
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: