弱鸡,C# System.Timers.Timer测试

来源:互联网 发布:http默认端口号 编辑:程序博客网 时间:2024/06/08 05:37
using System;using System.Threading;using System.Timers;namespace ConsoleApplication1{    class Program    {        static System.Timers.Timer timer;        private static int index = 0;        static void Main(string[] args)        {            Thread thread = new Thread(NewThread);            thread.Start();            timer = new System.Timers.Timer(10000) {Enabled = true};            timer.Elapsed += Print;            Console.Read();        }        private static void NewThread()        {            Console.WriteLine("进入线程,开始睡25S" + DateTime.Now.ToLocalTime());            Thread.Sleep(25000);            Console.WriteLine("25S睡过,停止timer" + DateTime.Now.ToLocalTime());            timer.Stop();            Thread.Sleep(3000);            Console.WriteLine("再睡3S,再启动timer" + DateTime.Now.ToLocalTime());            timer.Start();        }        private static void Print(object sender, ElapsedEventArgs e)        {            Console.WriteLine("index = 第" + index + " 次   " + DateTime.Now.ToLocalTime());            index++;        }    }}

原创粉丝点击