/// <summary> /// 延迟调用 /// </summary> /// <param name="method"></param> /// <param name="delayTime"></param> public static void DelayInvoke(Action method, int delayTime = 2000) { RegularInvoke(method, delayTime, false); } /// <summary> /// 定时调用 /// </summary> /// <param name="method"></param> /// <param name="delayTime"></param> /// <param name="aotoReset"></param> public static void RegularInvoke(Action method, int delayTime, bool aotoReset = true) { System.Timers.Timer timer = new System.Timers.Timer(delayTime); timer.Elapsed += new System.Timers.ElapsedEventHandler((a, b) => { method(); }); //执行一次 false timer.AutoReset = aotoReset; //是否执行System.Timers.Timer.Elapsed事件 timer.Enabled = true; }
//延迟调用 DelayInvoke(() => { //DO }, 1800); //定时调用 RegularInvoke(() => { //DO }, 1800);