C#多線程編程執行個體-線程與表單互動的程式碼分析

來源:互聯網
上載者:User
C#多線程編程執行個體 線程與表單互動

代碼:

    public partial class Form1 : Form    {        //聲明線程數組        Thread[] workThreads = new Thread[10];        public Form1()        {            InitializeComponent();        }        //此委託允許非同步調用為Listbox添加Item        delegate void AddItemCallback(string text);        //這種方法示範如何線上程安全的模式下調用Windows表單上的控制項。        private void AddItem(string text)        {            if (this.listBox1.InvokeRequired)            {                AddItemCallback d = new AddItemCallback(AddItem);                this.Invoke(d, new object[] { text });            }            else            {                this.listBox1.Items.Add(text);            }        }        //資料擷取方法        public void DataGet()        {            while (true)            {                AddItem("ok");                Thread.Sleep(1000);            }            //如果此方法退出了,那麼這個線程也就退出了        }        /// <summary>        /// 啟動線程        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_Click(object sender, EventArgs e)        {            //迴圈建立並啟動線程執行            for (int i = 0; i < workThreads.Length; i++)            {                if (workThreads[i] == null)                {                    //如果線程不存在,則建立                    workThreads[i] = new Thread(new ThreadStart(DataGet));                    workThreads[i].Name = i.ToString();                    workThreads[i].Start();                }                else                {                    //已經存在,如果沒有運行,則啟動                    if (workThreads[i].ThreadState == ThreadState.Aborted || workThreads[i].ThreadState == ThreadState.Stopped)                    {                        workThreads[i] = new Thread(new ThreadStart(DataGet));                        workThreads[i].Name = i.ToString();                        workThreads[i].Start();                    }else{                        workThreads[i].Start();                    }                }            }        }        /// <summary>        /// 停止線程        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button2_Click(object sender, EventArgs e)        {            //迴圈停止線程執行            for (int i = 0; i < workThreads.Length; i++)            {                //如果線程存在,並且狀態不是停止也不是終止的,則終止該線程                if (workThreads[i] != null && workThreads[i].ThreadState != ThreadState.Stopped && workThreads[i].ThreadState != ThreadState.Aborted)                {                    workThreads[i].Abort();                }            }        }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.