【c#】控制台程序编写计时器

用System.Timers类实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace ConsoleApplication3
{
   class Program
   {
       static void Main(string[] args)
       {
           Timer t = new Timer();
           t.Elapsed += new ElapsedEventHandler(OnTimedEvent);
           t.Interval = 1000;
           t.Enabled = true;
           Console.Read();
       }

       private static void OnTimedEvent(object source, ElapsedEventArgs e)
       {
           Console.Clear();
           Console.WriteLine(DateTime.Now);
       }
   }
}

运行效果:


用线程实现

TimerTest类:

class TimerTest
    {
        //线程名
        string _ThreadName;
        public string ThreadName
        {
            get { return _ThreadName; }
            private set { _ThreadName = value; }
        }

        //时间间隔
        int _TimeInterval;
        public int TimeInterval
        {
            get { return _TimeInterval; }
            set { _TimeInterval = value; }
        }

        //当前计时器是否启用 true:启用 false:不启用
        bool _Enabled;
        public bool Enabled
        {
            get { return _Enabled; }
            set { _Enabled = value; }
        }
        //每隔一段时间需要运行的事件
        public delegate void TickEventHandler();
        public event TickEventHandler TickEvent;
        /// <summary>
        /// 建立一个定时器
        /// </summary>
        /// <param name="ThreadName">线程名</param>
        /// <param name="TimeInterval">时间间隔</param>
        public TimerTest(string ThreadName,int TimeInterval = int.MaxValue)
        {
            this.ThreadName = ThreadName;
            this.TimeInterval = TimeInterval;
            this.Enabled = false;
        }

        /// <summary>
        /// 定期执行事件
        /// </summary>
        public void Run()
        {
            while (true)
            {
                //如果当前计时器并未启用,则每隔一段事件监测是否被启用
                if (!this.Enabled)
                {
                    Thread.Sleep(100);
                    continue;
                }
                //触发事件TickEvent
                if (TickEvent != null)
                {
                    TickEvent();
                }
                //休眠一定的事件,等待下一个循环
                Thread.Sleep(TimeInterval % 100);
                for (int temp = 0; temp < TimeInterval/100; temp++)
                {
                    Thread.Sleep(100);
                    if (!this.Enabled)
                    {
                        break;
                    }
                }
            }
        }
    }

调用程序:

 static void Main(string[] args)
        {           
            TimerTest tt = new TimerTest("timer_test",1000);
            tt.Enabled = true;
            tt.TickEvent += TestHandler;
            Thread timer = new Thread(tt.Run);
            timer.Start();
            Console.ReadKey();
        }

        static void TestHandler()
        {
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }

运行效果:







  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值