private void TimerElapsedHandler(object sender, ElapsedEventArgs e) { _updateTime = _updateTime.AddDays(1.0); CalculateTimerInterval(); Elapsed?.Invoke(sender, e); }
IEnumerator DespawnTimeout(float seconds) { yield return(new WaitForSeconds(seconds)); Elapsed?.Invoke(this); GameObject.Destroy(gameObject); }
public void Tick() { if (Enabled) { Elapsed?.Invoke(0, 0); } }
public unsafe void Start() { lock (stateLock) { if (!Enabled) { if (IsSupportedNatively(Interval)) { var caps = new TimeCaps(); _ = timeGetDevCaps(ref caps, (uint)sizeof(TimeCaps)); var clampedInterval = Math.Clamp((uint)Interval, caps.wPeriodMin, caps.wPeriodMax); _ = timeBeginPeriod(clampedInterval); timerId = timeSetEvent(clampedInterval, 1, callbackDelegate, IntPtr.Zero, EventType.TIME_PERIODIC | EventType.TIME_KILL_SYNCHRONOUS); Enabled = true; } else { Log.Write("Timer", "Unsupported interval detected, will use fallback timer. Expect high CPU usage", LogLevel.Warning); fallbackTimer = new FallbackTimer { Interval = Interval }; fallbackTimer.Elapsed += () => Elapsed?.Invoke(); fallbackTimer.Start(); Enabled = true; } } } }
public Timer() { base.Elapsed += (o, e) => { Elapsed?.Invoke(this, EventArgs.Empty); }; }
/// <summary> /// This method is called by _taskTimer. /// </summary> /// <param name="state">Not used argument</param> private void TimerCallBack(object state) { _taskTimer.Locking(t => { if (!_isRunning || _performingTasks) { return; } t.Change(Timeout.Infinite, Timeout.Infinite); _performingTasks = true; } ); try { Elapsed.Invoke(this, new EventArgs()); } catch { //do nothing. } finally { _taskTimer.Locking(t => { _performingTasks = false; if (_isRunning) { t.Change(Period, Timeout.Infinite); } Monitor.Pulse(_taskTimer); }); } }
public void Update(long deltaTime) { // Nothing to do if (!IsActive) { return; } // Decrement remaining time if (Remaining > 0) { Remaining -= deltaTime; } if (Remaining <= 0) { // No more remaining time for this slice --Value; Elapsed?.Invoke(this, EventArgs.Empty); if (Value != 0) { // Subtract current remaining time from specified interval because it can be negative Remaining = Interval - Remaining; } else { // We're done! Stopped?.Invoke(this, EventArgs.Empty); Remaining = 0; } } }
private void ThreadLoop() { while (_threadLoopEnabled) { // If we are not enabled, or we have more than the CoarseSleepThreshold ms left until we need to invoke, sleep for 1 ms while (_threadLoopEnabled && (!_elapsedEventEnabled || _stopwatch.ElapsedMilliseconds + CoarseSleepThreshold < _nextInvoke)) { Thread.Sleep(1); } // If we are not yet ready to invoke, then be kind to other threads & let them have some pie, but don't sleep as we are close while (_threadLoopEnabled && _stopwatch.ElapsedMilliseconds < _nextInvoke) { Thread.Sleep(0); } if (_threadLoopEnabled && _elapsedEventEnabled) { long time = _stopwatch.ElapsedMilliseconds; this.TimeSinceLastTick = time - _lastTick; _lastTick = time; Elapsed?.Invoke(null, null); _nextInvoke = time + (int)this.Interval; } } }
private void TimerCallBack(object state) { lock (_taskTimer) { if (!_running || _performingTasks) { return; } _taskTimer.Change(Timeout.Infinite, Timeout.Infinite); _performingTasks = true; } var elapsedEventArgs = state as ElapsedEventArgs; try { Elapsed?.Invoke(this, elapsedEventArgs); } catch (Exception e) { Console.WriteLine(e); } finally { lock (_taskTimer) { _performingTasks = false; if (_running) { _taskTimer.Change(Period, Timeout.Infinite); } Monitor.Pulse(_taskTimer); } } }
/// <summary> /// This method is called by _taskTimer. /// </summary> /// <param name="state">Not used argument</param> private void timerCallBack(object state) { lock (_lock) { if (!_running || _performingTasks) { return; } _taskTimer.Change(Timeout.Infinite, Timeout.Infinite); _performingTasks = true; } try { Elapsed?.Invoke(this, EventArgs.Empty); } finally { lock (_lock) { _performingTasks = false; if (_running) { _taskTimer.Change(Period, Timeout.Infinite); } Monitor.Pulse(_taskTimer); } } }
/// <summary> /// If there are any subscribers - raise the Elapsed event. /// </summary> /// <param name="sender">the sender to include within the event's event-args</param> internal void InvokeElapsed(object sender) { if (Elapsed != null) { Elapsed.Invoke(sender, m_timerEventArgs); } }
/// <summary> /// This method is called by _taskTimer. /// </summary> /// <param name="state">Not used argument</param> private void TimerCallBack(object state) { lock (_timer) { if (!_isRunning || _performingTasks) { return; } _timer.Change(Timeout.Infinite, Timeout.Infinite); _performingTasks = true; } try { Elapsed?.Invoke(this, new EventArgs()); } catch { } finally { lock (_timer) { _performingTasks = false; if (_isRunning) { _timer.Change(Period, Timeout.Infinite); } Monitor.Pulse(_timer); } } }
public void ElapsedAction() { if (Elapsed != null) { Elapsed.Invoke(this, null); } }
private void Callback(object state) { lock (this) { _tickCount++; Elapsed?.Invoke(this, TimeSpan.FromMilliseconds(_tickCount * Period.TotalMilliseconds)); var elapsed = DateTime.Now - _startAt; var millisShouldBe = _tickCount * Period.TotalMilliseconds; var millisIs = elapsed.TotalMilliseconds; var timeDiff = millisShouldBe - millisIs; if (timeDiff > Period.TotalMilliseconds) { timeDiff = Period.TotalMilliseconds; } if (timeDiff < -Period.TotalMilliseconds) { timeDiff = -Period.TotalMilliseconds; } if (_enabled) { _timer.Change( (int)(Period.TotalMilliseconds + timeDiff), (int)Period.TotalMilliseconds ); } } }
private void ThreadMain() { float nextNotification = 0; float elapsedMilliseconds; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); while (this.runTimer) { nextNotification += Interval; while ((elapsedMilliseconds = (float)stopWatch.Elapsed.TotalMilliseconds) < nextNotification) { Thread.Yield(); } if (elapsedMilliseconds - nextNotification >= IgnoreEventIfLateBy) { continue; } Elapsed?.Invoke(); } stopWatch.Stop(); }
private void OnElapsed(object source, ElapsedEventArgs e) { Elapsed?.Invoke(this, EventArgs.Empty); // reset timer in case we modified it by Delay timer.Interval = interval; }
private void ExecuteTimer() { float nextTrigger = 0f; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while (isRunning) { nextTrigger += interval; float elapsed; while (true) { elapsed = ElapsedHiRes(stopwatch); float diff = nextTrigger - elapsed; if (diff <= 0f) { break; } if (diff < 1f) { Thread.SpinWait(10); } else if (diff < 5f) { Thread.SpinWait(100); } else if (diff < 15f) { Thread.Sleep(1); } else { Thread.Sleep(10); } if (!isRunning) { return; } } float delay = elapsed - nextTrigger; Elapsed?.Invoke(delay); // restarting the timer in every hour to prevent precision problems if (stopwatch.Elapsed.TotalHours >= 1d) { stopwatch.Restart(); nextTrigger = 0f; } } stopwatch.Stop(); }
public void Start(TimeSpan interval) { Xamarin.Forms.Device.StartTimer(interval, () => { Elapsed?.Invoke(this, UtcNow); return(true); }); }
private void baseElapsed(object sender, object e) { if (!AutoReset) { Stop(); } Elapsed?.Invoke(this, new EventArgs()); }
public void Tick() { if ((Time -= GameTime.DeltaTime) <= 0) { Time += Interval; Elapsed.Invoke(this, new TimerElapsedEventArgs()); } }
private void TimerElapsed(object sender, ElapsedEventArgs args) { _remainingTime = _remainingTime - 1; if (_remainingTime == 0 && _player is not null) { Elapsed?.Invoke(this, _player); } }
public new void Start() { Stopped = false; if (!EnableOverride && Enabled && Elapsed != null) { Elapsed.Invoke(this, new EventArgs() as ElapsedEventArgs); } }
private void ExecuteTimer() { float nextTrigger = 0f; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while (isRunning) { nextTrigger += interval; float elapsed; while (true) { elapsed = ElapsedHiRes(stopwatch); float diff = nextTrigger - elapsed; if (diff <= 0f) { break; } if (diff < 1f) { Thread.SpinWait(10); } else if (diff < 5f) { Thread.SpinWait(100); } else if (diff < 15f) { Thread.Sleep(1); } else { Thread.Sleep(10); } if (!isRunning) { return; } } float delay = elapsed - nextTrigger; Elapsed?.Invoke(this, new HiResTimerElapsedEventArgs(delay)); if (stopwatch.Elapsed.TotalHours >= 1d) { stopwatch.Restart(); nextTrigger = 0f; } } stopwatch.Stop(); }
/// <summary> /// Handles the Elapsed event of the Timer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param> private void Timer_Elapsed(object sender, ElapsedEventArgs e) { Elapsed?.Invoke(this, e); if (AutoReset) { timer.Interval = CalculateInterval(triggerTime); timer.Start(); } }
IEnumerator OnElapsed() { while (true) { yield return(new WaitForSeconds(period)); Elapsed?.Invoke(this); } }
private void OnElapsed(CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } Elapsed?.Invoke(this, cancellationToken); }
// Update is called once per frame void Update() { timeLeft -= Time.deltaTime; if (timeLeft < 0) { timeLeft = interval; Elapsed.Invoke(); } }
private void NotifyTimerElapsed(object source, ElapsedEventArgs e) { Elapsed?.Invoke(); if (_timer?.AutoReset == false) { _timer?.Stop(); _timer?.Dispose(); _timer = null; } }
private void timerCallback(uint id, uint msg, ref uint userCtx, uint rsv1, uint rsv2) { //var handler = Elapsed; //if (handler != null) //{ // handler(this, EventArgs.Empty); //} Elapsed?.Invoke(this, EventArgs.Empty); }
private void OnTimerCallback(object context) { if (Elapsed == null) { return; } Elapsed.Invoke(this, Deadline, context); Elapsed = null; }