public void Run() { Stop(); Stoped = false; _activatedPlanetIds = new List <int>(); _timer = new Timer { Interval = DEFAULT_CYCLE_INTERVAL_MS, AutoReset = true }; _timer.Elapsed += (sender, args) => { if (_inProgress) { return; } _inProgress = true; var timer = (Timer)sender; var minTime = (int)Math.Floor(DEFAULT_CYCLE_INTERVAL_MS * 0.5); var maxTime = DEFAULT_CYCLE_INTERVAL_MS * 2; var nextDelay = _rand.Next(minTime, maxTime); timer.Interval = nextDelay; _provider.ContextAction(connection => { try { connection.OpenIfClosed(); _run(connection); return(true); } finally { connection.CloseIfOpened(); } }); }; _timer.Start(); }