/// <summary> /// CreateTimerId /// </summary> /// <param name="timeout"></param> /// <param name="repeat"></param> /// <param name="action"></param> /// <returns></returns> public string CreateTimerId(int timeout, int repeat = 0, Action <int, int> action = null) { string id = Guid.NewGuid().ToString(); var item = new MyTimerItemInfo { Id = id, Repeat = repeat, Timeout = timeout, CreateTime = CommonTool.GetMiscoTimeStamp(), CompletedHandler = action }; lock (_obj) { Items.Add(id, item); } return(id); }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { var ls = new List <dynamic>(); var timestamp = CommonTool.GetMiscoTimeStamp(); lock (_obj) { var ls1 = new List <string>(); foreach (var item in Items) { int n1 = 0; Int64 n = timestamp - item.Value.CreateTime; if (n > item.Value.Timeout) { n1 = (int)(n / item.Value.Timeout); if (n % item.Value.Timeout != 0) { n1++; } } if (n1 > 0) { ls.Add(new { Timeout = item.Value.Timeout, Count = n, Handler = item.Value.CompletedHandler }); if (n1 >= item.Value.Repeat) { ls1.Add(item.Key); } } } foreach (var item in ls1) { Items.Remove(item); } } try { foreach (var item in ls) { var fun = item.Handler as Action <int, int>; if (fun != null) { fun.Invoke((int)item.Timeout, (int)item.Count); } } } catch (Exception e1) { ConsoleHelper.WriteLine( ELogCategory.Fatal, string.Format("MyTimer.Timer_Elapsed Exception: {0}", e1.Message), true, e: e1 ); } Timer.Start(); }