private void AnalysisStrategy_RemainderCountUpdatedEvent(object sender, int e) { RemainderCountUpdatedEvent?.Invoke(this, e); }
public void Start(IEnumerable <StockTransactionModelExtern> stockBases) { if (_isStarted) { return; } _isStarted = true; _isStoped = false; _cacheStockTransactionModelExterns = stockBases as ConcurrentQueue <StockTransactionModelExtern>; if (_cacheStockTransactionModelExterns == null) { throw new ArgumentException($"'stockBases' type must be ConcurrentQueue<StockTransactionModelExtern>."); } _configModel = ConfigJsonHelper.GetConfigModel(); CheckConfig(); _itemWorkCount = _configModel.QuickUpStrategyConfigData.StockMaxCountEachGroup; int threadCount = _configModel.QuickUpStrategyConfigData.ThreadCount; this._forwardSeconds = _configModel.QuickUpStrategyConfigData.ForwardSeconds; this._afterSeconds = _configModel.QuickUpStrategyConfigData.AfterSeconds; this._dealAmountThreshold = (int)_configModel.QuickUpStrategyConfigData.DealAmountThreshold; this._quickUpThreshold = _configModel.QuickUpStrategyConfigData.QuickUpThreshold / 100; for (int ntask = 1; ntask <= threadCount; ntask++) { Task.Factory.StartNew(() => { //System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)ntask; var remainCount = 0; while (!_isStoped) { try { //剩余待处理的数量 int count = _cacheStockTransactionModelExterns.Count; if (remainCount != count) { remainCount = count; RemainderCountUpdatedEvent.Invoke(this, count); } bool isDequeueSuccess = _cacheStockTransactionModelExterns.TryDequeue(out var model); if (isDequeueSuccess) { if (!_modelsCaches.Any()) { _modelsCaches.TryAdd(0, new ConcurrentDictionary <string, ConcurrentQueue <StockTransactionModelExtern> >()); StockGroupWork(_modelsCaches[0], _modelsCaches.Count); } for (int i = 0; ; i++) { //如果字典已有此个股 if (_modelsCaches[i].ContainsKey(model.Code)) { _modelsCaches[i][model.Code].Enqueue(model); break; } //如果未包含此个股,且字典容量小于所设定的容量限制 if (_modelsCaches[i].Count < _itemWorkCount) { _modelsCaches[i].TryAdd(model.Code, new ConcurrentQueue <StockTransactionModelExtern>(new List <StockTransactionModelExtern> { model })); break; } //如果字典容量超过所设定的容量限制 if (!_modelsCaches.ContainsKey(i + 1)) { _modelsCaches.TryAdd(i + 1, new ConcurrentDictionary <string, ConcurrentQueue <StockTransactionModelExtern> >()); StockGroupWork(_modelsCaches[i + 1], _modelsCaches.Count); } continue; } } Thread.Sleep(1); } catch { } } }, TaskCreationOptions.LongRunning); } }