教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 C#中定时器控件Timer学习参考

C#中定时器控件Timer学习参考

发布时间:2016-03-25   编辑:jiaochengji.com
C#中的定时器控件Timer,分为三个类,今天为大家详细介绍,包括System.Windows.Forms、System.Threading.Timer、System.Timers.Timer类等,有需要的朋友参考下吧。

System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或 Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。

  System.Timers.Timer 和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的 Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。

例子,使用System.Timers.Timer类
 

System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;

代码如下:
 

复制代码 代码示例:

//timer应用举例
public void theout(object source, System.Timers.ElapsedEventArgs e)
{
  MessageBox.Show("OK!");
}

private void button1_Click(object sender, System.EventArgs e) ...{
  System.Timers.Timer t = new System.Timers.Timer(1 * 60 * 60 * 1000);
  t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
  t.Enabled = true;
}
private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e) ...{
  MessageBox.Show("hello");
}
//如果非要button 触发的话:
private void Form1_Load(object sender, System.EventArgs e) ...{
  System.Timers.Timer t = new System.Timers.Timer(1 * 60 * 60 * 1000);
  t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
  t.Enabled = true;
}
private void button1_Click(object sender, System.EventArgs e) ...{
  MessageBox.Show("hello");
}
private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e) ...{
  this.button1.PerformClick();
}

  应用中的timer和组件timer的不同

  服务器计时器、Windows 计时器和线程计时器
  在Visual Studio .NET和 .NET Framework 中有三种计时器控件:基于服务器的计时器,位于“工具箱”的“组件”选项卡上;基于 Windows 的标准计时器,位于“工具箱”的“Windows 窗体”选项卡上,以及仅可在编程时使用的线程计时器。基于 Windows 的计时器从 Visual Basic 的 1.0 版起就存在于该产品中并且基本上保持不变。该计时器已经为在 Windows 窗体应用程序中使用而进行了优化。基于服务器的计时器是传统的计时器为了在服务器环境上运行而优化后的更新版本。线程计时器是一种简单的、轻量级计时器,使用回调方法而不是事件,并由线程池线程提供。

  在 Win32 体系结构中有两种类型的线程:UI 线程和辅助线程。UI 线程绝大多数时间处于空闲状态,等待消息循环中的消息到来。一旦接收到消息,它们就进行处理并等待下一个消息到来。另外,辅助线程用来执行后台处理而且不使用消息循环。Windows 计时器和基于服务器的计时器在运行时都使用 Interval 属性。线程计时器的时间间隔在 Timer 构造函数中设置。计时器的设计目的各不相同,它们的线程处理明确地指出了这一点:

  Windows 计时器是为单线程环境设计的,其中,UI 线程用于执行处理。Windows 计时器的精度限定为 55 毫秒。这些传统计时器要求用户代码有一个可用的 UI 消息泵,而且总是在同一个线程中操作,或者将调用封送到另一个线程。对于 COM 组件来说,这样会降低性能。

  基于服务器的计时器是为在多线程环境下与辅助线程一起使用而设计的。由于它们使用不同的体系结构,因此基于服务器的计时器可能比 Windows 计时器精确得多。服务器计时器可以在线程之间移动来处理引发的事件。
对消息不在线程上发送的方案中,线程计时器是非常有用的。例如,基于 Windows 的计时器依赖于操作系统计时器的支持,如果不在线程上发送消息,与计时器相关的事件将不会发生。在这种情况下,线程计时器就非常有用。
Windows 计时器位于 System.Windows.Forms 命名空间中,服务器计时器位于 System.Timers 命名空间中,而线程计时器位于 System.Threading 命名空间中。

  Timer 组件

  Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发 Elapsed 事件的周期性间隔。然后可以操控此事件以提供定期处理。例如,假设您有一台关键性服务器,必须每周 7 天、每天 24 小时都保持运行。可以创建一个使用 Timer 的服务,以定期检查服务器并确保系统开启并在运行。如果系统不响应,则该服务可以尝试重新启动服务器或通知管理员。

  基于服务器的 Timer 是为在多线程环境中用于辅助线程而设计的。服务器计时器可以在线程间移动来处理引发的 Elapsed 事件,这样就可以比 Windows 计时器更精确地按时引发事件。有关基于服务器的计时器的更多信息,请参见“基于服务器的计时器介绍”。

  基于 Interval 属性的值,Timer 组件引发 Elapsed 事件。可以处理该事件以执行所需的处理。例如,假设您有一个联机销售应用程序,它不断向数据库发送销售订单。编译发货指令的服务分批处理订单,而不是分别处理每个订单。可以使用 Timer 每 30 分钟启动一次批处理。

  当 AutoReset 设置为 false 时,Timer 只在第一个 Interval 过后引发一次 Elapsed 事件。若要保持以 Interval 时间间隔引发 Elapsed 事件,请将 AutoReset 设置为 true。

  Elapsed 事件在 ThreadPool 线程上引发。如果 Elapsed 事件的处理时间比 Interval 长,在另一个 ThreadPool 线程上将会再次引发此事件。因此,事件处理程序应当是可重入的。

每隔五秒钟在控制台上显示一次“Hello World!”的例子。
 

复制代码 代码示例:

using System;
using System.Timers;

public class Timer1
{
  public static void Main()
  {
    // Normally, the timer is declared at the class level, so
    // that it doesn't go out of scope when the method ends.
    // In this example, the timer is needed only while Main
    // is executing. However, KeepAlive must be used at the
    // end of Main, to prevent the JIT compiler from allowing
    // aggressive garbage collection to occur before Main
    // ends.
    System.Timers.Timer aTimer = new System.Timers.Timer();

    // Hook up the Elapsed event for the timer.
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    // Set the Interval to 2 seconds (2000 milliseconds).
    aTimer.Interval = 2000;
    aTimer.Enabled = true;

    Console.WriteLine("Press the Enter key to exit the program.");
    Console.ReadLine();

    // Keep the timer alive until the end of Main.
    GC.KeepAlive(aTimer);
  }

  // Specify what you want to happen when the Elapsed event is
  // raised.
  private static void OnTimedEvent(object source, ElapsedEventArgs e)
  {
    Console.WriteLine("Hello World!");
  }
}

您可能感兴趣的文章:
C# Timer定时器控件运行时需要修改系统时间的问题
C#中Timer定时器控件实例与原理解析
C#中timer类定时器控件的应用实例
C#中定时器控件Timer的简单用法
C#中定时器控件Timer学习参考
C#各种定时器Timer类的区别与使用介绍
C# timer 定时器类的用法举例
C#进度条ProgressBar和定时器Timer控件的应用举例
C#中Timer定时器控件的使用方法
c#进度条 progressBar的小例子

[关闭]
~ ~