项目中有个需求,需要在每天凌晨1点执行几个功能,代码怎么些呢?
平时经常用的那些timer,更多的是应用于间隔特定时长执行,而非在具体某个时间点执行。
//设定定时执行 setTaskAtFixedTime(); private void setTaskAtFixedTime() { DateTime now = DateTime.Now; DateTime oneOClock = DateTime.Today.AddHours(1.0); //凌晨1:00 if (now > oneOClock) { oneOClock = oneOClock.AddDays(1.0); } int msUntilFour = (int)((oneOClock - now).TotalMilliseconds); var t = new System.Threading.Timer(doAt1AM); t.Change(msUntilFour, Timeout.Infinite); } //要执行的任务 private void doAt1AM(object state) { //执行功能... //再次设定 setTaskAtFixedTime(); }
标签:now,每天,C#,凌晨,DateTime,oneOClock,setTaskAtFixedTime,执行 From: https://www.cnblogs.com/duhaoran/p/16857613.html