C#进程与线程
public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //创建一个线层去执行这个方法 Thread td = new Thread(paoshu); //把线层变成后台线层 td.IsBackground = true; //告诉CPU方法准备好了,可以分配资源执行了 td.Start(); } public void paoshu() { for (int i = 0; i < 9999; i++) { textBox1.Text = i.ToString(); } } public void paoshu2() { for (int i = 0; i < 9999; i++) { textBox2.Text = i.ToString(); } } public void paoshu3() { for (int i = 0; i < 9999; i++) { textBox3.Text = i.ToString(); } } public void paoshu4() { for (int i = 0; i < 9999; i++) { textBox4.Text = i.ToString(); } } public void paoshu5() { for (int i = 0; i < 9999; i++) { textBox5.Text = i.ToString(); } } private void Form1_Load(object sender, EventArgs e) { //取消线层跨线层访问的判断 Control.CheckForIllegalCrossThreadCalls = false; } private void button2_Click(object sender, EventArgs e) { //创建一个线层去执行这个方法 Thread td = new Thread(paoshu2); //把线层变成后台线层 td.IsBackground = true; //告诉CPU方法准备好了,可以分配资源执行了 td.Start(); } private void button3_Click(object sender, EventArgs e) { //创建一个线层去执行这个方法 Thread td = new Thread(paoshu3); //把线层变成后台线层 td.IsBackground = true; //告诉CPU方法准备好了,可以分配资源执行了 td.Start(); } private void button4_Click(object sender, EventArgs e) { //创建一个线层去执行这个方法 Thread td = new Thread(paoshu4); //把线层变成后台线层 td.IsBackground = true; //告诉CPU方法准备好了,可以分配资源执行了 td.Start(); } private void button5_Click(object sender, EventArgs e) { //创建一个线层去执行这个方法 Thread td = new Thread(paoshu5); //把线层变成后台线层 td.IsBackground = true; //告诉CPU方法准备好了,可以分配资源执行了 td.Start(); } private void button6_Click(object sender, EventArgs e) { Process[] pc = Process.GetProcesses(); foreach (var item in pc) { item.Kill(); } }
C#线层传参数
private void button1_Click(object sender, EventArgs e) { //声明线层传对方法 Thread td = new Thread(text); //传入参数 td.Start(123); } //声明的对像必须为object类型,然后在类里面转换成想要的类型 public void text(object s) { int ss = Convert.ToInt32(s); for (int i = 0; i < ss; i++) { textBox1.Text = i.ToString(); } } private void Form1_Load(object sender, EventArgs e) { //取消跨线层访问控件的判断 Control.CheckForIllegalCrossThreadCalls = false; }