经典案例, 每隔一分钟执行一次的定时任务, 用 thread+ while(true) 还是timer

经典案例, 每隔一分钟执行一次的定时任务, 用 thread+ while(true) 还是timer

1.

while(true)
{
//dosomething
System.Threading.Thread.Sleep(60 * 1000);
}


2.
一种是的等待一分钟,一种是到了一分钟之后触发执行某个事情。

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
}
原文地址:https://www.cnblogs.com/withoutaword/p/6763312.html