async Task SetupTimerAsync(Rule rule) { var timer = new Timer(); this.timerLock.EnterWriteLock(); try { Dictionary <Guid, Timer> timers; if (!this.timers.TryGetValue(rule.TransactionHash, out timers)) { timers = new Dictionary <Guid, Timer>(); this.timers.Add(rule.TransactionHash, timers); } timers.Add(rule.Id, timer); try { var remainingWaitingTime = await this.ruleRepository.GetRemainingWaitingTimeAsync(rule.Id, CancellationToken.None); timer.Elapsed += OnTimeout; timer.Start(remainingWaitingTime < TimeSpan.Zero ? TimeSpan.Zero : remainingWaitingTime, null, rule); } catch { timers.Remove(rule.Id); throw; } } finally { this.timerLock.ExitWriteLock(); } }
void StartTimer(Rule rule, TimeSpan timeout) { if (this.semaphore.CurrentCount > 0) { throw new InvalidOperationException("The semaphore must be held before a timer can start."); } var timer = new Timer(this.timerScheduler); var address = rule.AddressReservation.Address.Address; this.watchings.Add(address, new Watching(rule, timer)); try { timer.Elapsed += (sender, e) => { e.RegisterBackgroundTask( cancellationToken => OnTimeoutAsync((BitcoinAddress)e.Context, cancellationToken)); }; timer.Start(timeout, null, address); } catch { this.watchings.Remove(address); throw; } }