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)
                {
                        t1 = new Thread(this.th1);
                        t1.Start();
                }

                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)
                {
                        t2 = new Thread(this.th2);
                        t2.Start();
                }

                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();
                private void th1()
                {
                        while (true)
                        {
                                if (this.InvokeRequired)
                                {
                                        this.Invoke(new weituo(js1));
                                        Thread.Sleep(1000);
                                }
                                else
                                {
                                        js1();
                                }
                        }
                        
                }

                private void th2()
                {
                        while (true)
                        {
                                if (this.InvokeRequired)
                                {
                                        this.Invoke(new weituo(js2));
                                        Thread.Sleep(1000);
                                }
                                else
                                {
                                        js1();
                                }
                        }

                }

                private void js1()
                {
                        int i = 1;
                        if (i == 1)
                        {
                                listBox1.Items.Add("這裏是線程 1");
                        }    
                }

                private void js2()
                {
                        int i = 1;
                        if (i == 1)
                        {
                                listBox1.Items.Add("這裏是線程 2");
                        }
                }
        }
}
這裏包含了多線程的所有用法。
在這裏我沒有寫判斷,大家自己加進去好了,很簡單。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章