private void Update() { RemainingDuration -= Time.deltaTime; if (RemainingDuration <= 0f) { RemainingDuration = 0f; OnTimeElapsed.Invoke(); gameObject.SetActive(false); } }
// Update is called once per frame void Update() { TimeLeft = TimeLeft.Subtract(TimeSpan.FromSeconds(Time.deltaTime)); if (TimeLeft.CompareTo(TimeSpan.Zero) <= 0) { TimeLeft = TimeSpan.Zero; enabled = false; if (OnTimeElapsed != null) { OnTimeElapsed.Invoke(this, null); } } }
public void Update(GameTime gameTime) { if (!IsStopped) { TotalSeconds += gameTime.ElapsedGameTime.TotalSeconds; } if (!(TotalSeconds >= Interval)) { return; } OnTimeElapsed?.Invoke(this, new GameTimerEventArgs(TotalSeconds, Interval)); TotalSeconds = 0; if (!IsLooped) { Reset(); } }
private int count; // number of iterations - if it is negative it means an infinite loop (see below in constructor) #endregion Fields #region Constructors public Timer(OnTimeElapsed method, int interval, int count = -1) { if (interval <= 0) throw new ArgumentOutOfRangeException(); // interval cannot be negative or zero // initializes parameters this.count = count; this.intervalSeconds = interval; this.doWork = method; // creates separate thread that loops until it has been executed "count" number of times Thread run = new Thread(delegate() { while (this.count != 0) { Thread.Sleep(this.intervalSeconds * 1000); // waits some interval of seconds if (this.count > 0) this.count--; // counts down only if positive, otherwise it results to infinite loop this.doWork(); } }); run.Start(); // the thread must be started explicitly }
private void NotificationHandler(object sender, ElapsedEventArgs e) { OnTimeElapsed?.Invoke(this, e); }
private void ShedulerExecute(object sender, ElapsedEventArgs e) { Operation operation = (Operation)sender; OnTimeElapsed?.Invoke(operation, e); }