private void SetTimer(Guid processId, TimeSpan interval, string name) { _lock.AcquireWriterLock(_lockTimeout); try { IDictionary <string, DateTime> processTimers; var key = new TimerKey { ProcessId = processId, TimerName = name }; if (Timers.ContainsKey(key)) { Timers[key] = DateTime.UtcNow.Add(interval); } else { Timers.Add(key, DateTime.UtcNow.Add(interval)); } if (NeedSave != null) { NeedSave(this, EventArgs.Empty); } } finally { _lock.ReleaseWriterLock(); } RefreshTimer(); }
public object Timer(params object[] args) { if (!(args[0] is int || args[0] is string || args[0] is Symbol)) { // MessageBox.Show(string.Format("Malformed \"timer\" command. An integer or string ID is expected, but got a {0}.", what[1].GetType().Name), Application.ProductName); // continue; } var timerID = args[0].GetHashCode(); if (!Timers.ContainsKey(timerID)) { Timers.Add(timerID, new Timer()); } var timer = Timers[timerID]; var delay = Evaluate <int>(args[1]); if (delay <= 0) { timer.Action = null; timer.Interval = 0; timer.Repeat = false; return(timer); } timer.Action = args[0]; timer.Interval = timer.TimeLeft = delay; timer.Repeat = (args.Length == 3 && args[2] is Symbol && args[2].ToString() == "repeat"); return(timer); }
public async Task StopCharging() { if (this is Player) { var request = new { Category = "Events", Type = "StopCharging", Result = "ok" }; await(this as Player).GetSocketHandler().SendString(JSON.Encode(request)); } if (Timers.ContainsKey("ChargeTimer")) { Timers["ChargeTimer"].Close(); Timers["ChargeTimer"].Dispose(); Timers.Remove("ChargeTimer"); } IsCharging = false; var timer = new System.Timers.Timer(100); var startTime = DateTime.Now; var startValue = CurrentCharge; timer.Elapsed += (sen, arg) => { if (IsCharging == true || CurrentCharge == 0) { (sen as System.Timers.Timer).Stop(); (sen as System.Timers.Timer).Dispose(); Timers.Remove("ChargeTimer"); return; } if (this is Player) { var handler = (this as Player).GetSocketHandler(); if (handler == null) { (sen as System.Timers.Timer).Stop(); (sen as System.Timers.Timer).Dispose(); Timers.Remove("ChargeTimer"); return; } CurrentCharge = Math.Max(0, Math.Round(startValue - (DateTime.Now - startTime).TotalMilliseconds / 100 * .01 * MaxCharge)); dynamic update = new { Category = "Queries", Type = "StatUpdate", Stat = "CurrentCharge", Amount = CurrentCharge }; (this as Player).GetSocketHandler().SendString(JSON.Encode(update)); } }; Timers.Add("ChargeTimer", timer); timer.Start(); }
/// <summary> /// Starts the timer. /// </summary> /// <param name="name">The name.</param> /// <param name="timeout">The timeout.</param> public virtual void StartTimer(string name, int timeout) { if (timeout > 0) { Timer timer; if (Timers.ContainsKey(name)) { timer = Timers[name]; } else { timer = Timers[name] = Stack.CreateTimer(this); } timer.Delay = timeout; timer.Start(); } }
public ScheduleTimer(IScheduleManager scheduleManager, int taskID, int interval) : base(scheduleManager, taskID) { this._logger = scheduleManager.RootServices.GetRequiredService <ILogger <ScheduleTimer> >(); this._interval = interval; this._isFirstTime = true; lock (Timers) { if (Timers.ContainsKey(this.TaskID)) { throw new Exception("Попытка добавить второй таймер для той же задачи"); } Timers.Add(this.TaskID, this); } // first time delay is 10 seconds this._timer = new Timer(10000); this._timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed); this._swatch = new Stopwatch(); }
public void AddTimer(GameStartEventArgs args, int timer) { lock (Timers) { if (args.MinimumPlayers <= args.ReadyPlayers) { if (Timers.ContainsKey(args.RoomId)) { if (args.MinimumPlayers <= args.ReadyPlayers && args.Players.Length == args.ReadyPlayers) { Timers[args.RoomId] = 5; } } else { if (args.MinimumPlayers >= args.ReadyPlayers && args.Players.Length == args.ReadyPlayers) { Timers.Add(args.RoomId, 5); } else { Timers.Add(args.RoomId, timer); } } TimersData[args.RoomId] = args.Players; } else { if (Timers.ContainsKey(args.RoomId)) { RemoveTimer(args.RoomId); SendMessage(args.Players, -1); } } } }
private bool NtHasTimer(string name) => Timers.ContainsKey(name);
public async Task StartCharging() { if (this is Player) { var request = new { Category = "Events", Type = "StartCharging", Result = "ok" }; await(this as Player).GetSocketHandler().SendString(JSON.Encode(request)); } IsCharging = true; if (Timers.ContainsKey("ChargeTimer")) { Timers["ChargeTimer"].Close(); Timers["ChargeTimer"].Dispose(); Timers.Remove("ChargeTimer"); } var timer = new System.Timers.Timer(100); var startTime = DateTime.Now; var startValue = CurrentCharge; timer.Elapsed += (sen, arg) => { if (IsCharging == false) { (sen as System.Timers.Timer).Stop(); (sen as System.Timers.Timer).Dispose(); Timers.Remove("ChargeTimer"); return; } if (this is Player) { var handler = (this as Player).GetSocketHandler(); if (handler == null) { (sen as System.Timers.Timer).Stop(); (sen as System.Timers.Timer).Dispose(); Timers.Remove("ChargeTimer"); return; } CurrentCharge = Math.Min(MaxCharge, Math.Round(startValue + (DateTime.Now - startTime).TotalMilliseconds / 100 * .01 * MaxCharge)); dynamic update = new { Category = "Queries", Type = "StatUpdate", Stat = "CurrentCharge", Amount = CurrentCharge }; handler.SendString(JSON.Encode(update)); } foreach (var player in Storage.Current.Locations.Find(CurrentXYZ).GetNearbyPlayers().Where(p => p?.Player?.Name != Name)) { dynamic request = new { Category = "Events", Type = "CharacterCharging", Location = CurrentXYZ }; player.SendString(JSON.Encode(request)); } }; Timers.Add("ChargeTimer", timer); timer.Start(); }