·c#之Thread實現暫停繼續

來源:互聯網
上載者:User

標籤:initial   ace   tor   cross   interval   orm   簡單   ogr   forms   

暫停與繼續實現,可以使用Thread.Suspend和Thread.Resume而這兩個方法,在VS2010裡提示已經過時,不建議使用,在網上查閱了一些資料,發現有個事件通知的方法很好,事件通知的大致原理是,線程在執行過程中暫停,等到其他線程通知時才繼續執行下去,這樣的確是可以起到暫停與繼續的效果。但是,這種暫停是被動的,我需要的是主動暫停,即點下按鈕,線程暫停,再點下按鈕,線程繼續執行。

         最終,我想了一種比較另類的方法,大致思路如下:還是採用事件通知的方式,線上程中等待通知,直到來通知了才繼續執行,而主線程(表單線程)中使用一個計時器System.Windows.Forms.Timer 來不停的通知線程,如果計時器間隔時間設定的足夠小,基本上看不出停頓。此時,程式的暫停與繼續實現就很簡單了,相信大家已經想到了,只要在通過控制計時器的Stop()和Start()就可控制線程的暫停與繼續了。

 

using System;  using System.Windows.Forms;  using System.Threading;    namespace 線程暫停與繼續實現  {      public partial class Form1 : Form      {          //計時器          private System.Windows.Forms.Timer tm = new System.Windows.Forms.Timer();          //自動重設事件類別            //主要用到其兩個方法 WaitOne() 和 Set() , 前者阻塞當前線程,後者通知阻塞線程繼續往下執行          AutoResetEvent autoEvent = new AutoResetEvent(false);            public Form1()          {              InitializeComponent();              ProgressBar.CheckForIllegalCrossThreadCalls = false;              tm.Interval = 1;              tm.Tick += new EventHandler(tm_Tick);          }            //計時器 事件          void tm_Tick(object sender, EventArgs e)          {              autoEvent.Set(); //通知阻塞的線程繼續執行          }            //啟動          private void btnStart_Click(object sender, EventArgs e)          {              tm.Start();                Thread t = new Thread(DoWork);              t.Start();          }                    //線上程中執行的方法          private void DoWork()          {              while (progressBar1.Value < progressBar1.Maximum)              {                  progressBar1.PerformStep();                  autoEvent.WaitOne();  //阻塞當前線程,等待通知以繼續執行              }          }            //暫停          private void btnSuspend_Click(object sender, EventArgs e)          {              tm.Stop();          }            //繼續          private void btnResume_Click(object sender, EventArgs e)          {              tm.Start();          }      }  }  

 

·c#之Thread實現暫停繼續(轉)

聯繫我們

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