我有下面的代码,这会运行无限循环吗?
我试图每分钟安排一些事情,控制台应用程序应该持续运行,直到我关闭它。
class Program
{
static int curMin;
static int lastMinute = DateTime.Now.AddMinutes(-1).Minutes;
static void Main(string[] args)
{
// Not sure about this line if it will run continuously every minute??
System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimCallBack), null, 1000, 60000);
Console.Read();
timer.Dispose();
}
private static void TimCallBack(object o)
{
curMin = DateTime.Now.Minute;
if (lastMinute < curMin)
{
// Do my work every minute
lastMinute = curMin;
}
}
}
最佳答案
KISS - 或者你在竞争 Rube Goldberg奖? ;-)
static void Main(string[] args)
{
while(true)
{
DoSomething();
if(Console.KeyAvailable)
{
break;
}
System.Threading.Thread.Sleep(60000);
}
}
关于c# - 这段代码会无限循环吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2319984/