關於C#中的線程重啟的問題


首先不管是C#也好,還是java也好,對於已經Abort的線程是無法再次Start的,除非是聲明私有變量new一個新的線程,網上也有很多人說可以Suspend掛起線程,然后再Resume繼續,但是相信聰明的你們早就發現了,微軟官方已經將這兩個方法設為過時了,不推薦這么用,現在本人就分享一個本人覺得還算比較好用的方法:

     private List<Thread> _threadList = new List<Thread>();   //記錄產生的線程,可聲明為全局公共變量 

        public Form1()
        {
            InitializeComponent();
        }

        private void  DoWork()
        {
            for (int i = 0; i < 4; i++)
            {
                if (i == 0)
                {
                    Thread t = new Thread(() =>
                                              {
                                                  while (true)
                                                  {
                                                      BeginInvoke(new Action(() =>
                                                                                 {
                                                                                     label1.Text =
                                                                                         DateTime.Now.ToString(
                                                                                             "yyyy-MM-dd HH:mm:ss");
                                                                                 }));
                                                      Thread.Sleep(10);
                                                  }
                                              });
                    _threadList.Add(t);
                    t.Start();
                }
                else if (i == 1)
                {
                    Thread t = new Thread(() =>
                    {
                        while (true)
                        {
                            BeginInvoke(new Action(() =>
                            {
                                label2.Text =
                                    DateTime.Now.AddDays(1).ToString(
                                        "yyyy-MM-dd HH:mm:ss");
                            }));
                            Thread.Sleep(10);
                        }
                    });
                    _threadList.Add(t);
                    t.Start();
                }
                else if (i ==2)
                {
                    Thread t = new Thread(() =>
                    {
                        while (true)
                        {
                            BeginInvoke(new Action(() =>
                            {
                                label3.Text =
                                    DateTime.Now.AddMonths(1).ToString(
                                        "yyyy-MM-dd HH:mm:ss");
                            }));
                            Thread.Sleep(10);
                        }
                    });
                    _threadList.Add(t);
                    t.Start();
                }
                else if (i == 3)
                {
                    Thread t = new Thread(() =>
                    {
                        while (true)
                        {
                            BeginInvoke(new Action(() =>
                            {
                                label4.Text =
                                    DateTime.Now.AddYears(1).ToString(
                                        "yyyy-MM-dd HH:mm:ss");
                            }));
                            Thread.Sleep(10);
                        }
                    });
                    _threadList.Add(t);
                    t.Start();
                }
            }
        }

        private void BtnStartClick(object sender, EventArgs e)
        {
            Thread _threadMain = new Thread(DoWork);
            _threadMain.IsBackground = true;
            _threadList.Add(_threadMain);
            _threadMain.Start();
        }

        private void BtnStopClick(object sender, EventArgs e)
        {            
            foreach (var t in _threadList)
            {
                t.Abort();
            }
            _threadList.Clear();
        }        

 以上的代碼很簡單,界面上兩個按鈕,4個Lable分別顯示4個時間,點擊開始按鈕,開啟4個線程,並把4個線程對象加到List集合中,點擊結束按鈕,將List中的4個線程對象全都Abort,並將List清空,如果想要重啟的效果,則可以先調用Stop,然后再調Start即可,是不是很簡單吖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM