Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6320552
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: C#/.net

2013-04-02 09:01:30

原文地址:Timer进行定时界面处理 作者:dyli2000

一、System.Threading.Timer 与System.Windows.Forms.Timer

    System.Threading.Timer 是一个简单的轻量计时器,它使用回调方法并由线程池线程提供服务。不建议将其用于 Windows 窗体,因为其回调不在用户界面线程上进行(硬要使用的话只能通过委托的方式进行操作界面元素处理)。

    System.Windows.Forms.Timer 是用于 Windows 窗体的更佳选择。要获取基于服务器的计时器功能,可以考虑使用 System.Timers.Timer,它可以引发事件并具有其他功能。

 

二、案例测试说明

1、现需要在界面实现一个倒计时功能。效果如下:

image

图1


案例代码:

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;

  10. namespace CountDownCase
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public delegate void DelegateSetControls(Object state);
  15.         public int timeCount = 10;
  16.         public System.Windows.Forms.Timer timer2;

  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             timer2 = new System.Windows.Forms.Timer();
  21.             /* This sentence can't be used many times. */
  22.             timer2.Tick += new EventHandler(TimerEventProcessor);
  23.         }

  24.         /*********************---------------------------------------------------------------------------------------------------------------
  25.         * Related operation of timer1
  26.         *-------------------------------------------------------------------------------------------------------------------*****************/
  27.         private void timer1_Tick(object sender, EventArgs e)
  28.         {
  29.             this.label1.Text = "CountDown Beginning......";
  30.             if (timeCount != 0)
  31.             {
  32.                 this.label2.Text = timeCount.ToString() + "Second";
  33.                 timeCount--;
  34.             }
  35.             else
  36.             {
  37.                 this.label2.Text = "0 Second";
  38.                 this.label1.Text = "The countdown is over......";
  39.                 timer1.Stop();
  40.             }
  41.         }

  42.         private void button1_Click(object sender, EventArgs e)
  43.         {
  44.             timeCount = 10;
  45.             timer1.Start();
  46.         }

  47.         /*********************------------------------------------------------------------------------------------------------------------------
  48.         * related operation of timer2
  49.         *-------------------------------------------------------------------------------------------------------------------********************/
  50.         public int timeCountTimer2 = 10;
  51.         private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
  52.         {
  53.             if (timeCountTimer2 != 0)
  54.             {
  55.                 this.label3.Text = timeCountTimer2.ToString() + "second";
  56.                 timeCountTimer2--;
  57.             }
  58.             else
  59.             {
  60.                 this.label3.Text = "0 second";
  61.                 this.label4.Text = "Ready for operation......";
  62.                 this.timer2.Stop();
  63.             }
  64.         }

  65.         private void button2_Click(object sender, EventArgs e)
  66.         {
  67.             timeCountTimer2 = 10;
  68.             this.timer2.Interval = 1000;
  69.             this.timer2.Start();
  70.             this.label4.Text = "CountDown has begun......";
  71.         }

  72.     }
  73. }
CountDownCase.zip


2、关键代码分析

窗体控件法使用Timer的步骤:

(1)、拖控件

(2)、设置Timer的几个属性

image

图2


(3)、编写timer的Tick事件

image

图3



  1. private void timer1_Tick(object sender, EventArgs e)
  2. {
  3.     this.label1.Text = "CountDown Beginning......";
  4.     if (timeCount != 0)
  5.     {
  6.         this.label2.Text = timeCount.ToString() + "Second";
  7.         timeCount--;
  8.     }
  9.     else
  10.     {
  11.         this.label2.Text = "0 Second";
  12.         this.label1.Text = "The countdown is over......";
  13.         timer1.Stop();
  14.     }
  15. }



代码法创建Timer的步骤麻烦些:

(1)、创建好对象


  1. timer2 = new System.Windows.Forms.Timer();


(2)、编写Tick事件委托函数并赋值


  1. timer2.Tick += new EventHandler(TimerEventProcessor);
  2.  
  3. private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
  4. {
  5.     if (timeCountTimer2 != 0)
  6.     {
  7.         this.label3.Text = timeCountTimer2.ToString() + "second";
  8.         timeCountTimer2--;
  9.     }
  10.     else
  11.     {
  12.         this.label3.Text = "0 second";
  13.         this.label4.Text = "Ready for operation......";
  14.         this.timer2.Stop();
  15.     }
  16. }


(3)、设置定时触发时钟的时间


  1. this.timer2.Interval = 1000;
  2. this.timer2.Start();


 

OVER!

阅读(646) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~