private void TimerElapsedHandler(object sender, ElapsedEventArgs e)
        {
            _updateTime = _updateTime.AddDays(1.0);
            CalculateTimerInterval();

            Elapsed?.Invoke(sender, e);
        }
Exemplo n.º 2
0
    IEnumerator DespawnTimeout(float seconds)
    {
        yield return(new WaitForSeconds(seconds));

        Elapsed?.Invoke(this);
        GameObject.Destroy(gameObject);
    }
Exemplo n.º 3
0
 public void Tick()
 {
     if (Enabled)
     {
         Elapsed?.Invoke(0, 0);
     }
 }
Exemplo n.º 4
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;
             }
         }
     }
 }
Exemplo n.º 5
0
 public Timer()
 {
     base.Elapsed += (o, e) =>
     {
         Elapsed?.Invoke(this, EventArgs.Empty);
     };
 }
Exemplo n.º 6
0
        /// <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);
                });
            }
        }
Exemplo n.º 7
0
        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;
                }
            }
        }
Exemplo n.º 8
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;
                }
            }
        }
Exemplo n.º 9
0
        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);
                }
            }
        }
Exemplo n.º 10
0
        /// <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);
                }
            }
        }
Exemplo n.º 11
0
 /// <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);
     }
 }
Exemplo n.º 12
0
        /// <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);
                }
            }
        }
Exemplo n.º 13
0
 public void ElapsedAction()
 {
     if (Elapsed != null)
     {
         Elapsed.Invoke(this, null);
     }
 }
Exemplo n.º 14
0
        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
                        );
                }
            }
        }
Exemplo n.º 15
0
        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();
        }
Exemplo n.º 16
0
        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();
        }
Exemplo n.º 18
0
 public void Start(TimeSpan interval)
 {
     Xamarin.Forms.Device.StartTimer(interval, () =>
     {
         Elapsed?.Invoke(this, UtcNow);
         return(true);
     });
 }
Exemplo n.º 19
0
 private void baseElapsed(object sender, object e)
 {
     if (!AutoReset)
     {
         Stop();
     }
     Elapsed?.Invoke(this, new EventArgs());
 }
Exemplo n.º 20
0
 public void Tick()
 {
     if ((Time -= GameTime.DeltaTime) <= 0)
     {
         Time += Interval;
         Elapsed.Invoke(this, new TimerElapsedEventArgs());
     }
 }
Exemplo n.º 21
0
 private void TimerElapsed(object sender, ElapsedEventArgs args)
 {
     _remainingTime = _remainingTime - 1;
     if (_remainingTime == 0 && _player is not null)
     {
         Elapsed?.Invoke(this, _player);
     }
 }
Exemplo n.º 22
0
 public new void Start()
 {
     Stopped = false;
     if (!EnableOverride && Enabled && Elapsed != null)
     {
         Elapsed.Invoke(this, new EventArgs() as ElapsedEventArgs);
     }
 }
Exemplo n.º 23
0
        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();
        }
Exemplo n.º 24
0
 /// <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();
     }
 }
Exemplo n.º 25
0
    IEnumerator OnElapsed()
    {
        while (true)
        {
            yield return(new WaitForSeconds(period));

            Elapsed?.Invoke(this);
        }
    }
Exemplo n.º 26
0
        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();
     }
 }
Exemplo n.º 28
0
 private void NotifyTimerElapsed(object source, ElapsedEventArgs e)
 {
     Elapsed?.Invoke();
     if (_timer?.AutoReset == false)
     {
         _timer?.Stop();
         _timer?.Dispose();
         _timer = null;
     }
 }
Exemplo n.º 29
0
        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);
        }
Exemplo n.º 30
0
        private void OnTimerCallback(object context)
        {
            if (Elapsed == null)
            {
                return;
            }

            Elapsed.Invoke(this, Deadline, context);
            Elapsed = null;
        }