private void SimulatedTimer(ShortTimerController controller, BarSize barSize) { while (controller.Active) { SendData(barSize); System.Threading.Thread.Sleep(controller.Interval); } }
public RealTimeFromHistoricalDataSource(IHistoricalDataSource historicalDataSource, DataLocation dataLocation, bool rthOnly, bool saveDataToStorage, DateTime startingDate, DateTime endingDate, double?overrideTimerInterval = null) { Name = "Simulate real time from " + historicalDataSource.Name; _dataLocation = dataLocation; _rthOnly = rthOnly; _saveDataToStorage = saveDataToStorage; _startingDate = startingDate; _endingDate = endingDate; _historicalDataSource = historicalDataSource; _historicalDataSource.Error += (sender, args) => { _logger.Log(LogLevel.Error, $"Error while requesting historical data for real time simulation: {args.ErrorCode} {args.ErrorMessage}"); }; _historicalDataSource.HistoricalDataArrived += (sender, args) => { _historicalData.TryAdd(args.Request.RequestID, args.Data); }; var barSizes = Enum.GetValues(typeof(BarSize)).Cast <BarSize>(); SimulatedTimerAsync caller = new SimulatedTimerAsync(SimulatedTimer); foreach (var barSize in barSizes) { if (barSize > BarSize.OneDay) { break; } double interval = overrideTimerInterval ?? barSize.ToTimeSpan().TotalMilliseconds; // The system is not able to simulate a timer with less than ca. 900 ms (dependes on the system). // It will be moustly higher than the expected waiting time. So we simulate the timer as a loop here. if (interval < 1000) { ShortTimerController controller = new ShortTimerController { Active = false, Interval = (int)interval, Caller = caller }; _shortTimers.TryAdd(barSize, controller); } else { var timer = new Timer(interval); timer.Elapsed += TimerElapsed; _timers.TryAdd(barSize, timer); } } }