C#建立線程三種方式

來源:互聯網
上載者:User

1、利用 System.Timers.Timer 建立線程,同時重新整理介面顯示,利用SynchronizingObject 的屬性,出現卡死狀態。

System.Timers.Timer t = new System.Timers.Timer();         private void button_Click(object sender, EventArgs e)
        {
           t.Interval = 100;
           t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
           t.Enabled = true;
           t.SynchronizingObject = label1;
        }
        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                System.Threading.Thread.Sleep(50);
                label1.Text = i.ToString();
            }
            t.Enabled = false;
        }

2、利用ystem.Threading.Thread建立線程,重新整理介面,介面能夠即時顯示
        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.Thread th =  new System.Threading.Thread(new System.Threading.ThreadStart(Run));
            th.Start();
            label1.Text = "";
        }
        void Run()
        {
            for (int i = 0; i < 100; i++)
            {
                Call(i);
            }
            t.Stop();
        }
        delegate void UpCallBack(int i);
        void Call(int i)
        {
            if (this.InvokeRequired)
            {
                UpCallBack up = new UpCallBack(Call);
                this.Invoke(up, new Object[] { i });
            }
            else
            {
                System.Threading.Thread.Sleep(200);
                label1.Text = i.ToString();
            }
        }

3、利用 System.Timers.Timer 建立線程,同時重新整理介面顯示,出現卡死狀態。
        private void button2_Click(object sender, EventArgs e)
        {
            System.Timers.Timer timer = new System.Timers.Timer();            
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 5000;
        }
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                Call(i);
            }
        }
3、利用 System.Windows.Forms.Timer 建立線程,同時重新整理介面顯示,出現卡死狀態。

        private void button3_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Timer tr = new System.Windows.Forms.Timer();
            tr.Interval = 5000;
            tr.Tick += new EventHandler(tr_Tick);
            tr.Start();
        }
        void tr_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                Call(i);
            }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.