经典案例, 每隔一分钟执行一次的定时任务, 用 thread+ while(true) 还是timer
1.
while ( true ) { //dosomething System.Threading.Thread.Sleep(60 * 1000); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | private static System.Timers.Timer timers = new System.Timers.Timer(60 * 1000); static QueueIndex() { timers.AutoReset = true ; timers.Enabled = true ; timers.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Call); timers.Start(); } private static void Timer_Call( object sender, System.Timers.ElapsedEventArgs e) { //dosomething } |