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; } }
private void CheckConnection(System.Timers.ElapsedEventArgs e) { do { // 列表为空,表示不处理。这时没有自动重连 if (SessionTimeList == null || SessionTimeList.Count == 0) { break; } var stl = SessionTimeList.Where(x => x.Contains(e.SignalTime.DayOfWeek)).ToList(); if (stl.Count == 0) { break; } bool bTryConnect = true; TimeSpan ts = e.SignalTime.TimeOfDay; SessionTimeItem st_current = null; SessionTimeItem st_next = null; foreach (var st in stl) { // 如果当前时间在交易范围内,要开启重连 // 如果当前时间不在交易范围内,要主动断开 if (ts < st.SessionStart) { // 停 bTryConnect = false; st_next = st; break; } else if (ts <= st.SessionEnd) { // 启动 bTryConnect = true; st_current = st; break; } else { // 停 bTryConnect = false; st_next = st; } } if (bTryConnect) { // 没有连接要连上,有连接要设置时间 if (!IsConnected) { xlog.Info($"当前[{e.SignalTime.TimeOfDay}]在交易时段[{st_current}],主动连接"); _Connect(false); } // 初始化查询间隔 SetApiReconnectInterval(_ReconnectInterval); nDisconnectCount = 0; } else { // 关闭查询间隔 SetApiReconnectInterval(0); // 由于定时器设置的是20秒,所以这里正好是5分钟显示一次 if (nDisconnectCount % (3 * 5) == 0) { xlog.Info($"当前[{e.SignalTime.TimeOfDay}]在非交易时段,主动断开连接,下次要连接的时段为[{st_next}](仅限当日)"); // 要断开连接 _Disconnect(false); } ++nDisconnectCount; } } while (false); }