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++;
}
}
}