要实现的功能是 : 每隔10分钟启动程序, 启动后做一些消耗时间的操作, 但不管这些操作需要多少时间(一般不会超过10分钟) , 程序仍然准时在10分钟后启动 。
while (true) { int startTime = Environment.TickCount File.AppendAllText(strCurrentPath + @"\TimerLog.txt", "begin " + DateTime.Now.ToString() + "\r\n"); //do some work which will spent time spendTime(); int timeBeforeSleep = Environment.TickCount int consumedTime = timeBeforeSleep - startTime Thread.Sleep(600000 - consumedTime); // sleep 10 mins } // while
详见
http://*.com/questions/21398097/how-to-set-exactly-time-period