很少在控制台上用定时器,最近要用到,百度了一遍文章。很不错,摘下来,作备忘

关于C#中timer类 在C#里关于定时器类就有3个
1.定义在System.Windows.Forms里
2.定义在System.Threading.Timer类里
3.定义在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控件。他们的缺点是不支持直接的拖放,需要手工编码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Collections; namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(theout); //到达时间的时候执行事件;
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Interval = ;
aTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
aTimer.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
}
public void theout(object source, System.Timers.ElapsedEventArgs e)
{
ArrayList AutoTask = new ArrayList();
AutoTask.Add("8:30:00");
AutoTask.Add("9:30:00");
AutoTask.Add("10:30:00");
AutoTask.Add("11:34:15"); for (int n = ; n < ; n++)
{
if (DateTime.Now.ToLongTimeString().Equals(AutoTask[n]))
{
MessageBox.Show("现在时间是" + AutoTask[n]);
}
}
}
}

最新文章

  1. kgcd ,fmod,fgcd
  2. 体验了微信小程序,发现安卓用户终于把果粉“碾压”了一次
  3. nav ccsss
  4. UINavigationController详解三(转)ToolBar
  5. C标签之forEach
  6. js 性能优化整理之 高频优化
  7. 深入浅出Redis-redis底层数据结构(下)
  8. 深入浅出Ajax(三)
  9. ZooKeeper介绍,安装,配置文件解析
  10. 编程语言的基础——搞定JavaIO
  11. C++中const对象和非const对象调用成员函数问题
  12. 给定一个数列a1,a2,a3,...,an和m个三元组表示的查询,对于每个查询(i,j,k),输出ai,ai+1,...,aj的升序排列中第k个数。
  13. OpenDialog文件多选
  14. Windbg程序调试系列3-线程阻塞问题
  15. Error: Cannot find module &#39;babel-runtime/regenerator&#39;
  16. 【Android】详解Android动画之Interpolator插入器
  17. elasticsearch数据备份还原
  18. 解決中文地址Uri.IsWellFormedUriString返回false
  19. Android中使用progurad混淆代码
  20. CentOS 开发环境准备

热门文章

  1. 一些常用的jQuery插件
  2. POJ 2983 Is the Information Reliable?(差分约束系统)
  3. maven 依赖排除
  4. http://www.linuxidc.com/Linux/2015-02/114265.htm
  5. pmtest1.asm pmtest2.asm pmtest5.asm 这几个比较重要.
  6. Git push错误non-fast-forward后的冲突解决
  7. c++模板注意事项
  8. C#日期格式精确到毫秒以及上下午
  9. c#使用XSLT将xml文档转换为html文档
  10. STL 优先队列