该代码示例的核心是演示:让Timer只有本次任务执行完毕后才开始下次任务。
Timer示例代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TestTimer { class Program { static System.Timers.Timer timer1 = new System.Timers.Timer(); static void Main(string[] args) { timer1.AutoReset = true; timer1.Interval = 2000; timer1.Enabled = true; timer1.Elapsed += Timer1_Elapsed; while (true) { Thread.Sleep(5000); Console.WriteLine("运行中:" + Thread.CurrentThread.ManagedThreadId); } } private static void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { WriteLog.InserLogs("开始执行!"); timer1.Enabled = false; myTask(); timer1.Enabled = true; WriteLog.InserLogs("结束执行!"); } public static void myTask() { Random random = new Random(); int num = random.Next(1, 4); int sleepTime = 3000; if (num == 1) { sleepTime = 3000; } else if (num == 2) { sleepTime = 5000; } else if (num == 3) { sleepTime = 8000; } Thread.Sleep(sleepTime); WriteLog.InserLogs("耗时任务执行了。耗时:" + sleepTime); } } }
日志代码:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestTimer { public class WriteLog { /// <summary> /// 记录日志 /// </summary> /// <param name="ErrorMsg"></param> public static void InserLogs(string ErrorMsg) { try { string LogPath = System.AppDomain.CurrentDomain.BaseDirectory + "\SystemLog\"; if (!Directory.Exists(LogPath)) { Directory.CreateDirectory(LogPath); } using (StreamWriter sw = new StreamWriter(LogPath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true, System.Text.Encoding.UTF8)) { sw.Write(DateTime.Now.ToString() + ":INFO----"); sw.WriteLine(ErrorMsg); sw.WriteLine("--------------------------------------------- "); sw.Close(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } } }
耗时任务的耗时是随机的,上次任务执行完毕才会执行下次任务。
执行日志:
2020/8/3 19:36:34:INFO----开始执行!
---------------------------------------------
2020/8/3 19:36:42:INFO----耗时任务执行了。耗时:8000
---------------------------------------------
2020/8/3 19:36:42:INFO----结束执行!
---------------------------------------------
2020/8/3 19:36:44:INFO----开始执行!
---------------------------------------------
2020/8/3 19:36:47:INFO----耗时任务执行了。耗时:3000
---------------------------------------------
2020/8/3 19:36:47:INFO----结束执行!
---------------------------------------------
2020/8/3 19:36:49:INFO----开始执行!
---------------------------------------------
2020/8/3 19:36:57:INFO----耗时任务执行了。耗时:8000
---------------------------------------------
2020/8/3 19:36:57:INFO----结束执行!
---------------------------------------------
2020/8/3 19:36:59:INFO----开始执行!
---------------------------------------------
2020/8/3 19:37:07:INFO----耗时任务执行了。耗时:8000
---------------------------------------------
2020/8/3 19:37:07:INFO----结束执行!
---------------------------------------------
2020/8/3 19:37:09:INFO----开始执行!
---------------------------------------------
2020/8/3 19:37:14:INFO----耗时任务执行了。耗时:5000
---------------------------------------------
2020/8/3 19:37:14:INFO----结束执行!
---------------------------------------------
2020/8/3 19:37:16:INFO----开始执行!