public async Task Dispose() { // Verify that [Dispose()] stops the timer. var ticks = 0; var timer = new AsyncTimer( async() => { ticks++; await Task.CompletedTask; }); timer.Start(TimeSpan.FromSeconds(1)); await Task.Delay(TimeSpan.FromSeconds(4.5)); Assert.True(ticks == 5); timer.Dispose(); ticks = 0; await Task.Delay(TimeSpan.FromSeconds(4.5)); Assert.True(ticks == 0); // Verify that calling [Dispose()] on an already disposed timer // does not throw an exception. timer.Dispose(); }
private async Task AsyncTimerElapsed(object sender, EventArgs eventArgs) { // ReSharper disable once ConvertIfStatementToSwitchStatement if (Recurring && !_timer.AutoReset) { _timer.Dispose(); _timer = new AsyncTimer(new TimeSpan(24, 0, 0), true); //create a timer that has the correct interval InitTimer(); } else if (!Recurring) // timer is not going to fire anymore, dispose this object { Dispose(); } // execute the scheduled task last await _task(this); }
protected override void DisposeManagedResources() { _commitTimer.Dispose(); foreach (Owned <EngineRunner> runner in _runners.Values) { runner.Dispose(); } _runners.Clear(); }
/// <summary> /// Stop the heartbeats. /// </summary> private void StopProcessorTimer() { if (_timer == null) { return; } _timer.Dispose(); _timer = null; }
public void Stop() { paused = false; if (_tcpServer != null) { _tcpServer.Stop(); _tcpServer = null; } DateTimeService.Instance.HourChanged -= OnHourChanged; if (_scanTimer != null) { _scanTimer.Stop(); _scanTimer.Dispose(); _scanTimer = null; } }
public void Errors() { // Check error detection. var timer = new AsyncTimer(async() => await Task.CompletedTask); Assert.Throws <ArgumentException>(() => timer.Start(TimeSpan.FromSeconds(-1))); Assert.Throws <InvalidOperationException>(() => timer.Start()); timer.Dispose(); Assert.Throws <ObjectDisposedException>(() => timer.Start(TimeSpan.FromSeconds(1))); Assert.Throws <ObjectDisposedException>(() => timer.Stop()); timer = new AsyncTimer(); Assert.Throws <InvalidOperationException>(() => timer.Start(TimeSpan.FromSeconds(1))); timer.Dispose(); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public override void Dispose() { _cleaner.Dispose(); lock (_lock) _queue.Clear(); }