public Task Start() { return(Task.Factory.StartNew(() => { if (Token == null) { Token = new CancellationTokenSource(); } AskRepositoryTask = AskRepository.Start(Token); OpenAsksByBuyerCommdoity.Clear(); OpenAsks.Clear(); var commodities = CommodityRepository.GetCommodities(); Parallel.ForEach(commodities, c => OpenAsksByBuyerCommdoity.AddOrUpdate(c.CommodityID, new ConcurrentDictionary <int, ConcurrentDictionary <long, ThreadSafeAsk> >(), (k, o) => o)); Parallel.ForEach(OpenAsksByBuyerCommdoity, buyCommodityDictionary => { foreach (var commodity in commodities) { buyCommodityDictionary.Value.AddOrUpdate(commodity.CommodityID, new ConcurrentDictionary <long, ThreadSafeAsk>(), (k, o) => o); } }); Parallel.ForEach(AskRepository.GetAsks(), ask => { ThreadSafeAsk tAsk = CreateAsk(new ThreadSafeAsk(ask)); foreach (var commodityBuyID in tAsk.CommodityBuyID) { foreach (var commoditySellID in tAsk.CommoditySellID) { OpenAsksByBuyerCommdoity[commodityBuyID][commoditySellID].AddOrUpdate(tAsk.AskID, tAsk, (k, o) => o); } } OpenAsks.AddOrUpdate(tAsk.AskID, tAsk, (k, o) => o); }); })); }
public void Dispose() { if (Token != null) { Token.Cancel(false); } if (AskProcessorTask != null) { AskProcessorTask.Wait(); } if (AskRepositoryTask != null) { AskRepositoryTask.Wait(); } OpenAsks.Clear(); OpenAsksByBuyerCommdoity.Clear(); }