c# 多線程傳遞參數Demo (一個參數傳遞)

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);
                }
                
        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章