void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { lock (this) { _Timer.Enabled = false; do { // 列表为空,表示不处理。这时没有自动重连 if (SessionTimeList == null || SessionTimeList.Count == 0) { break; } bool bTryConnect = true; SessionTimeItem st_current = null; SessionTimeItem st_next = null; foreach (var st in SessionTimeList.ToList()) { // 如果当前时间在交易范围内,要开启重连 // 如果当前时间不在交易范围内,要主动断开 TimeSpan ts = e.SignalTime.TimeOfDay; if (ts < st.SessionStart) { // 停 bTryConnect = false; st_next = st; } else if (ts <= st.SessionEnd) { // 启动 bTryConnect = true; st_current = st; break; } else { // 停 bTryConnect = false; st_next = st; } } if (bTryConnect) { // 没有连接要连上,有连接要设置时间 if (!IsConnected) { xlog.Info("当前[{0}]在交易时段[{1}],主动连接", e.SignalTime.TimeOfDay, st_current); _Connect(false); } // 初始化查询间隔 SetApiReconnectInterval(_ReconnectInterval); nDisconnectCount = 0; } else { // 关闭查询间隔 SetApiReconnectInterval(0); // 由于定时器设置的是20秒,所以这里正好是5分钟显示一次 if (nDisconnectCount % (3 * 5) == 0) { xlog.Info("当前[{0}]在非交易时段,主动断开连接,下次要连接的时段为[{1}]", e.SignalTime.TimeOfDay, st_next); // 要断开连接 _Disconnect(false); } ++nDisconnectCount; } } while (false); // 查询持仓和资金 QueryAccountPosition_OnTimer(); _Timer.Enabled = true; } }
void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { _Timer.Enabled = false; do { // 列表为空,表示不处理。这时没有自动重连 if (SessionTimeList == null || SessionTimeList.Count == 0) { break; } bool bTryConnect = true; foreach (var st in SessionTimeList.ToList()) { // 如果当前时间在交易范围内,要开启重连 // 如果当前时间不在交易范围内,要主动断开 TimeSpan ts = e.SignalTime.TimeOfDay; if (ts < st.SessionStart) { // 停 bTryConnect = false; } else if (ts <= st.SessionEnd) { // 启动 bTryConnect = true; break; } else { // 停 bTryConnect = false; } } if (bTryConnect) { // 没有连接要连上,有连接要设置时间 if (!IsConnected || (!IsConnected && !IsDisconnected)) { xlog.Info("当前[{0}]在交易时段,主动连接", e.SignalTime.TimeOfDay); _Connect(false); } foreach (var item in ApiList) { if (item.UseType > 0 && item.Api != null) { item.Api.ReconnectInterval = _ReconnectInterval; } } } else { foreach (var item in ApiList) { if (item.Api != null) { item.Api.ReconnectInterval = 0; } } if (IsConnected || (!IsConnected && !IsDisconnected)) { xlog.Info("当前[{0}]在非交易时段,主动断开连接", e.SignalTime.TimeOfDay); // 要断开连接 _Disconnect(false); } } }while(false); // 查询持仓和资金 if (IsApiConnected(_QueryApi)) { _QueryAccountCount -= (int)_Timer.Interval / 1000; if (_QueryAccountCount <= 0) { ReqQueryField query = default(ReqQueryField); query.PortfolioID1 = DefaultPortfolioID1; query.PortfolioID2 = DefaultPortfolioID2; query.PortfolioID3 = DefaultPortfolioID3; query.Business = DefaultBusiness; _QueryApi.ReqQuery(QueryType.ReqQryTradingAccount, ref query); _QueryAccountCount = _QueryAccountInterval; } _QueryPositionCount -= (int)_Timer.Interval / 1000; if (_QueryPositionCount <= 0) { ReqQueryField query = default(ReqQueryField); query.PortfolioID1 = DefaultPortfolioID1; query.PortfolioID2 = DefaultPortfolioID2; query.PortfolioID3 = DefaultPortfolioID3; query.Business = DefaultBusiness; _QueryApi.ReqQuery(QueryType.ReqQryInvestorPosition, ref query); _QueryPositionCount = _QueryPositionInterval; } } _Timer.Enabled = true; }