I'm trying to capture tick level data from a Binance Aggregated data stream. As soon as the date changes, a function processes the data from the previous day. I'm using Task.Run to create another thread as new data for the current day is still streaming in and needs to be captured. However, I'm getting a 'Collection was modified enumeration operation may not execute.' in the foreach loop of the function that processes the data. I'm not sure why tradeData would be modified after it is passed to the processing function? Not sure if it matters but I'm capturing about 40 different symbols on separate threads.
private static void Start()
{
foreach (SymbolData symbol in symbolData)
{
Task.Run(() => SubscribeToSymbol(symbol.symbol));
}
}
private static void SubscribeToSymbol(string symbol)
{
Dictionary<decimal, decimal> tradeData = new Dictionary<decimal, decimal>();
DateTime lastTrade = DateTime.UtcNow;
try
{
var socketClient = new BinanceSocketClient();
socketClient.FuturesUsdt.SubscribeToAggregatedTradeUpdates(symbol, data =>
{
if (data.TradeTime.Date > lastTrade.Date)
{
Task.Run(() => ProcessData(symbol, lastTrade.Date, tradeData));
tradeData.Clear();
}
lastTrade = data.TradeTime;
if (tradeData.ContainsKey(data.Price))
{
tradeData[data.Price] = tradeData[data.Price] + data.Quantity;
}
else
{
tradeData[data.Price] = data.Quantity;
}
});
}
catch { }
}
private static void ProcessData(string symbol, DateTime date, Dictionary<decimal, decimal> tradeData)
{
foreach (var price in tradeData)
{
//Error: Collection was modified enumeration operation may not execute.
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…