• C#定时器的用法


    关于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控件。他们的缺点是不支持直接的拖放,需要手工编码。
    1.
    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 = 1000;
                 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 = 0; n < 4; n++)
                {
                    if (DateTime.Now.ToLongTimeString().Equals(AutoTask[n]))
                    {
                        MessageBox.Show("现在时间是" + AutoTask[n]);
                    }
                }
     
       
            }

    2.

    C#.net 定时器
    最近需要用到一个定时器,设定当 程序 到某时刻 执行某段代码。
     
    using System;
    using System.Timers;
    namespace 定时器ConsoleApplication1
    {
    class Class1
    { 
      [STAThread] 
      static void Main(string[] args)
      {
       System.Timers.Timer aTimer = new System.Timers.Timer(); 
       aTimer.Elapsed += new ElapsedEventHandler(TimeEvent); 
       // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
       aTimer.Interval = 1000; 
       aTimer.Enabled = true; 
       Console.WriteLine("按回车键结束程序";
       Console.WriteLine(" 等待程序的执行......";
       Console.ReadLine();
      }
      // 当时间发生的时候需要进行的逻辑处理等
      //    在这里仅仅是一种方式,可以实现这样的方式很多.
      private static void TimeEvent(object source, ElapsedEventArgs e) 
      {  
       // 得到 hour minute second  如果等于某个值就开始执行某个程序。
       int intHour   = DateTime.Now..Hour;
       int intMinute = DateTime.Now.Minute;
       int intSecond = DateTime.Now.Second;
       // 定制时间; 比如 在10:30 :00 的时候执行某个函数
       int iHour   = 10;
       int iMinute = 30;
       int iSecond = 00;
       // 设置  每秒钟的开始执行一次
       if( intSecond == iSecond )
       {
        Console.WriteLine("每秒钟的开始执行一次!";
       }
       // 设置 每个小时的30分钟开始执行
       if( intMinute == iMinute && intSecond == iSecond )
       {
        Console.WriteLine("每个小时的30分钟开始执行一次!";
       }
        
       // 设置 每天的10:30:00开始执行程序
       if( intHour == iHour && intMinute == iMinute  && intSecond == iSecond )
       {
        Console.WriteLine("在每天10点30分开始执行!";
       }
      } 
    }
    } 
        }
    }
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    Android 6.0 运行时权限处理完全解析
    Android 6.0 运行时权限处理
    史上比较用心的纯代码实现 AutoLayout
    hdu 4268 Alice and Bob(multiset|段树)
    在单调的队列
    Python re正则表达式
    minihomepage.exe 百度视频迷你主页
    POJ 2914 Minimum Cut 最小割图论
    移植kl档,但调用默认Generic.kl解决的方法
    iOS 删除黑色边框线导航栏&amp;删除搜索框的阴影边界线和中黑色文本输入框 - 解
  • 原文地址:https://www.cnblogs.com/BluceLee/p/13825504.html
Copyright © 2020-2023  润新知