小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

三種Timer的使用

 悟靜 2013-01-01

三種Timer的使用

在.net中有三種計時器,一是System.Windows.Forms命名空間下的Timer控件,它直接繼承自Componet;二是System.Timers命名空間下的Timer類。

Timer控件:Timer控件只有綁定了Tick事件,和設(shè)置Enabled=True后才會自動計時,停止計時可以用Stop()控制,通過Stop()停止之后,如果想重新計時,可以用Start()方法來啟動計時器。Timer控件和它所在的Form屬于同一個線程;

 

System.Timers.Timer類:定義一個System.Timers.Timer對象,綁定Elapsed事件,通過Start()方法啟動計時,通過Stop()方法或者Enable=False停止計時。AutoReset屬性設(shè)置是否重復(fù)計時。Elapsed事件綁定就相當(dāng)另開了一個線程,也就是說在Elapsed綁定的事件里不能訪問其它線程里的控件。

 

System.Threading.Timer:定義該類時,主要有四個參數(shù)。TimerCallBack,一個返回值為void,參數(shù)為object的委托,也是計時器執(zhí)行的方法。Object state,計時器執(zhí)行方法的的參數(shù)。 int dueTime,調(diào)用 callback 之前延遲的時間量(以毫秒為單位)。指定 Timeout.Infinite 以防止計時器開始計時。指定零 (0) 以立即啟動計時器。

int Period,調(diào)用 callback 的時間間隔(以毫秒為單位)。指定 Timeout.Infinite 可以禁用定期終止。

 

在這三種計時器中,第一種計時器和所在的Form處于同一個線程,因此執(zhí)行的效率不高。而第二種和第三中計時器執(zhí)行的方法都是新開一個線程,所以執(zhí)行效率比第一種計時器要好。因此在使用計時器時,建議使用第二種和第三種。

下面是三中定時器使用的例子

1)Timer控件  

  public partial class Timer : Form
    {

        int count = 0;
        public Timer()
        {
            InitializeComponent();

            //timer控件可用
            this.timer1.Enabled = true;

           //設(shè)置timer控件的Tick事件觸發(fā)的時間間隔
            this.timer1.Interval = 1000;

            //停止計時
            this.timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            count += 1;
            this.tbTimer.Text = count.ToString();
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            //開始計時
            this.timer1.Start();
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            //停止計時
            this.timer1.Stop();
           
        }

    }

2)System.Timers.Timer

 

public partial class Timer : Form
    {
        int count = 0;

        private System.Timers.Timer timer = new System.Timers.Timer();

        public Timer()
        {
            InitializeComponent();

            //設(shè)置timer可用
            timer.Enabled = true;
           
            //設(shè)置timer
            timer.Interval = 1000;

            //設(shè)置是否重復(fù)計時,如果該屬性設(shè)為False,則只執(zhí)行timer_Elapsed方法一次。
            timer.AutoReset = true;

            timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }

        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            count += 1;
            SetTB(count.ToString());
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            timer.Start();
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }

        private delegate void SetTBMethodInvok(string value);

        private void SetTB(string value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetTBMethodInvok(SetTB), value);
            }
            else
            {
                this.tbTimer.Text = value;
            }
        }
    }

 

3) System.Threading.Timer

 

    public partial class Timer : Form
    {
        int count = 0;
        System.Threading.Timer timerThr;
        private delegate void SetTBMethodInvoke(object state);

        public Timer()
        {
            InitializeComponent();

            //初始化一個計時器,一開始不計時,調(diào)用Callback的時間間隔是500毫秒
            timerThr = new System.Threading.Timer(new TimerCallback(SetTB), null, Timeout.Infinite, 500);
        }

        public void SetTB(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetTBMethodInvoke(SetTB), value);
            }
            else
            {
                count += 1;
                this.tbTimer.Text = count.ToString();
            }
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            //開始計時
            timerThr.Change(0, 500);
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            //停止計時
            timerThr.Change(Timeout.Infinite, 500);
        }
    }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多