C#

来源:互联网 发布:大连 大数据 编辑:程序博客网 时间:2024/05/20 20:56

之前因为实验室需要撰写过一个C#实用文档。把他分成专题,分享到博客上来;
当时看了很多资料和博客,如果哪位原博主觉得被冒犯,特此道歉!
请联系我,我将把您的博文连接添加至参考文献列表。

—————因为实例中有一个Timer的控件,因此我建立的都是WinForm工程————————
********timer控件的使用在最后,用 一张图片简介使用方法************



在.net常用的定时器类有下面三种:

  • System.Windows.Forms.Timer类 
  • System.Threading.Timer类
  • System.Timers.Timer类

使用定时器时需要设定参数,如间断时间、定时器计溢出后的回调函数、延时、开始等,定时器的的主要方法有开始、终止等,不同的定时器实现上述的方法会有一些差异,本章会针对具体的定时器一一举例说明

Class Name Function System.Timers.Timer Server Timers 基于服务器的计时器,位于”工具箱”的“组件”选项卡上 System.Threading.Timer Thread Timers 在编程时使用的线程计时器 System.Windows.Forms.Timer Windows Timers 基于 Windows 的标准计时器,”工具箱”的”Windows 窗体”选项卡上;

—————————————————————————————————–

1. System.Windows.Forms.Timer

从这个定时器的命名空间可以看出,.net设计这个定时器的目的是为了方便程序员在Window Form中使用的定时器。当一个System.Windows.Forms.Timer类被构造时,当前定时器会和当前线程进行关联。而当计时器的计满后,一个定时器消息将被插入到当前线程的消息队列中。当前线程逐一处理消息中的所有消息,并一一派发给各自的处理方法。这样的机制和利用工作者进程定时有很大的区别,System.Windows.Forms.Timer类型并没有涉及多线程的操作,定时器的设置、定时方法的执行都在同一个线程之上。这就意味着System.Windows.Forms.Timer并不能准确计时,尤其当消息阻塞时,定时器的误差将会更大,因为定时器消息只能等待在前面的所有消息处理完后才能得到处理。但是因为System.Windows.Forms.Timer类型的定时器并不涉及多线程的操作,因此是线程安全的,不会发生回调方法重入的问题。

使用步骤如下

1.  System.Windows.Forms.Timer myTimer =                new System.Windows.Forms.Timer();//实例化 2.  myTimer.Tick += new EventHandler(函数名); //给timer挂起事件3.  myTimer.Enabled = true;   //使timer可用4.  myTimer.Interval = n;      //设置时间间隔,以毫秒为单位5.  myTimer.Stop();            //如果要暂停计时则使用Stop()方法6.  myTimer.Enabled = false;  //若要停止使用timer,则使之不可用

System.Windows.Forms.Timer 例程

----------**System.Windows.Forms.Timer例程:**----------------using System.Windows.Forms;  namespace WindowsFormsApplication1  {      public partial class Form1 : Form      {          public Form1()          {              InitializeComponent();          }          //实例化一个timer          System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();          private void StartTimeBtn_Click(object sender, EventArgs e)//开始计时          {               //给timer挂起事件              myTimer.Tick  += new EventHandler(Callback);              //使timer可用              myTimer.Enabled = true;              //设置时间间隔,以毫秒为单位              myTimer.Interval = 1000;//1s          }          private void StopTimeBtn_Click(object sender, EventArgs e)//停止计时          {              //计时开始              myTimer.Stop();          }          //回调函数          private void Callback(object sender, EventArgs e)          {               //获取系统时间 20:16:16              textBox1.Text = DateTime.Now.ToLongTimeString().ToString();          }      }  }

—————————————————————————————————–

2 System.Threading.Timer

这个定时器类的使用相对复杂,但同时它也是最优化的一个定时器类型。System.Threading.Timer的定时方法在独立的线程上执行,定时时间更为准确。所有的对象有一个线程控制,当下一个定时到达时,该线程会负责在线程中获得一个新的工作者线程,用以执行相应的回调方法。虽然这个定时器是相对最优化的一个定时器类型,但是从其机制上来讲,其并不是线程安全的,可能会出现回调方法重入的问题。解释下方法重入,是一个有关多线程编程的概念,意思大概是:同一程序中,多个线程同时运行时,就可能发生同一个方法被多个进程同时调用的情况。当这个方法中存在一些非线程安全的代码时,方法重入会导致数据发生同步错误的bug。

主要使用步骤如下

1.  实例构造一个线程定时器。     System.Threading.Timer  mytimer =          new System.Threading.Timer(new  System.Threading.TimerCallback(timerCall), null, 0, 1000);------------------------------------------------------------- **|Timer构造函数资料|**   public Timer(    TimerCallback callback,   // TimerCallback 委托,表示要执行的方法。    object state,             //一个包含回调方法要使用的信息的对象,或者为 null。    int dueTime,       //调用 callback 之前延迟的时间量(以毫秒为单位)。                       //指定 Timeout.Infinite 可防止启动计时器。                       // 指定零 (0) 可立即启动计时器。                       //指定零 (-1) 表示本定时器被禁用。                       //Change(Int32,Int32)方法可以改写定时器参数    int period     //如果 period 为零 (0) 或 -1 毫秒,而且dueTime 为正                   //则只会调用一次 callback;----------------------------------------------------------------2.  编写timerCall回调函数格式:private void timerCall(Object  xxxxx) {……;……;}3.  使用Change(Int32, Int32)方法来修改定时器参数实现停止、重新开始等。4.  使用 Dispose() 方法释放定时器资源。

System.Threading.Timer 例程

----------**System.Threading.Timer 例程**----------------public void startTimer()  {    //定义一个对象    System.Threading.Timer timer = new System.Threading.Timer(               new System.Threading.TimerCallback(mytimer), null, -1, //不立即启动 1000);//1S定时器  }  //启动定时器,且时间间隔设置为500msprivate void button2_Click(object sender, EventArgs e){    timer.Change(0, 500);}//函数形式参数必须是object格式  public void mytimer (object a )  {    Console.WriteLine("你好");  }//关闭定时器private void button3_Click(object sender, EventArgs e){    timer.Dispose();}

==============================================================================

3 System.Timers.Timer类

这是一个相对较旧的类型,它和System.Threading.Timer一样,可以由工作者线程来执行回调方法,但同时它也可以在IDE环境中被拖到窗体控件上,这个时候它的行为非常类似于System.Windows.Forms.Timer类型,在消息过多时其定时并不准确。System.Timers.Timer可以视为System.Threading.Timer的一个包装,其类型设计相对古老,不建议使用该定时器。

System.Threading.Timer 例程

----------**System.Timers.Timer例程代码**----------------System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒  private void Form1_Load(){    t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);    t.AutoReset = false; //每到指定时间Elapsed事件是触发一次(false),还是一直触发(true)  }private void btnStart_Click(object sender, EventArgs e){    t.Enabled = true; //是否触发Elapsed事件      t.Start();}private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e){    //到达指定时间5秒触发该事件输出 HelloWorld!!!!      System.Diagnostics.Debug.WriteLine("HelloWorld!!!!");}private void btnStop_Click(object sender, EventArgs e){    t.Stop();    System.Diagnostics.Debug.WriteLine("未到指定时间5秒提前终结!!!");}

==============================================================================

4 timer控件的使用

位于”工具箱”的“组件”选项卡上的timer控件使用的是System.Windows.Form.Timer中的timer,基于消息机制实现。
Timer 控件的使用非常简单,从工具箱中拖拽控件应用到窗体上,在timer控件上右键属性,属性页配置见下图:

这里写图片描述

三个函数:

 - 启动:timer1.Start();  - 停止:timer1.Stop();  - 释放资源:timer1.Dispose();
原创粉丝点击