/// <summary> /// 今天指定时间做调用,hour(18:30填18.5),method(调用的函数),args(函数参数) /// </summary> /// <param name="hour"></param> /// <param name="method"></param> /// <param name="args"></param> public static void RegTodayTimerEvent(float hour, TimerMethod method, object args = null) { if (method == null) { return; } if (todayHandlers.hash.ContainsKey(method)) { return; } TodayTimerHandler handler = new TodayTimerHandler(); TimerEvent events = new TimerEvent(args); events.key = method; handler.method = method; handler.args = events; handler.hour = hour; System.DateTime now = System.DateTime.Now; int phour = (int)hour; int pminute = (int)(60 * (hour - phour)); if (now.Hour >= phour && now.Minute >= pminute) { handler.isTodayExe = true; } else { handler.isTodayExe = false; } handler.lastTime = System.DateTime.Now; todayHandlers.addElement(method, handler); }
public async Task UpdateHub(CommandContext ctx) { TimerMethod.UpdateHubInfo(true); DiscordMessage msg = await ctx.RespondAsync("TCHub info has been force updated."); await Task.Delay(10000).ContinueWith(f => msg.DeleteAsync()); }
/// <summary> /// 执行计时器回调函数; /// </summary> /// <param name="value"></param> protected static void runTodayTimerMethod(TodayTimerHandler value) { TimerMethod method = value.method; if (method != null) { method(value.args); } }
static void Main(string[] args) { TimerMethod testMethod = Method1; testMethod += Method2; var timer = new Timer(25, testMethod); timer.Run(); }
/// <summary> /// 执行计时器回调函数; /// </summary> /// <param name="value"></param> protected static void runTimerMethod(TimerHandler value) { try { TimerMethod method = value.method; if (method != null) { method(value.args); } } catch (System.Exception ex) { Log.Log.Server.Warning(ex.ToString()); } }
public static void Run() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("07. Timer with delegate"); Console.ForegroundColor = ConsoleColor.White; TimerMethod testDelegate = TestMethod; testDelegate += AnotherTestMetod; testDelegate += delegate { Console.WriteLine("I am Anonimus Method"); }; Timer testTimer = new Timer(5, 2, testDelegate); testTimer.Run(); }
//创建计时器; protected static System.Object create(bool useFrame, bool repeat, float delay, TimerMethod method, object args, bool cover = false) { System.Object key; if (cover) { //先删除相同函数的计时; clearTimer(method); key = method; } else { key = index++; } TimerHandler handler; if (pool.Count > 0) { handler = (TimerHandler)pool[pool.Count - 1]; pool.RemoveAt(pool.Count - 1); } else { handler = new TimerHandler(); } TimerEvent events = new TimerEvent(args); events.key = key; handler.userFrame = useFrame; handler.repeat = repeat; handler.delay = delay; handler.method = method; handler.args = events; handler.exeTime = delay + (useFrame ? currentFrame : currentTime); handlers.addElement(key, handler); count++; return(key); }
/**清理;*/ public void clear() { method = null; args = null; }
public Timer(byte ticks, int seconds, TimerMethod currentMethod) { this.Ticks = ticks; this.Interval = seconds; this.Method = currentMethod; }
/**定时执行一次(基于帧率); * @param delay 延迟时间(单位为帧); * @param method 结束时的回调方法; * @param args 回调参数; * @param cover 是否覆盖(true:同方法多次计时,后者覆盖前者。false:同方法多次计时,不相互覆盖); * @return cover=true时返回回调函数本身,cover=false时,返回唯一ID,均用来作为clearTimer的参数;*/ public static System.Object doFrameOnce(float delay, TimerMethod method, object args = null, bool cover = false) { return(create(true, false, delay, method, args, cover)); }
public Timer(int seconds, TimerMethod method) { this.Seconds = seconds; this.Method = method; }