private void CreateThread(ScanServiceSetting.ScanSetting setting) { Thread thread = new Thread(new ParameterizedThreadStart(GetScanMessageList)); thread.Name = setting.MessType + "_" + setting.OrderStatus.ToString(); thread.Start(setting); m_threadDic[thread.Name] = thread; }
private void GetScanMessageList(object setting) { string ikey = Guid.NewGuid().ToString(); ScanServiceSetting.ScanSetting scanSetting = (ScanServiceSetting.ScanSetting)setting; Process.Debug("m_scanConditionDic", "m_scanConditionDic", string.Format("scanSetting:【{0}】", scanSetting.ScanCount), ""); int scanIntervalTime = scanSetting.IntervalTime * (-1); ScanOrderCondition con = new ScanOrderCondition(); con.OrderBy = scanSetting.OrderBy; con.ScanOrderIdInit = scanSetting.ScanOrderIdInit; con.PageSize = scanSetting.ScanCount; con.Status = scanSetting.OrderStatus; con.Includes = string.Join(",", scanSetting.Include.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); DateTime endTime = GetDateTimeNow(ikey); con.StartTime = endTime.AddSeconds(scanIntervalTime); con.EndTime = endTime; Handle.Debug(ikey, "订单扫描开始", "GetScanMessageList", scanSetting.MessType, string.Format("条件参数【{0}】,订单扫描初开始,间隔:{1}", JsonConvert.SerializeObject(con), scanIntervalTime.ToString()), ""); try { System.Diagnostics.Stopwatch execTime = new System.Diagnostics.Stopwatch(); while (true) { execTime.Restart(); ikey = Guid.NewGuid().ToString(); List <NotifyOrderEntity> orderList = GetOrdersList(con); if (orderList != null && orderList.Count > 0) { foreach (NotifyOrderEntity orderEnt in orderList) { if (!IsSendingDataCache(orderEnt, scanSetting.MessType)) { string errMsg = ""; NotifyMessage message = new NotifyMessage(); message.MessageKey = SetMessageKey(orderEnt, scanSetting.MessType); message.SourceFrom = "OrderScan手动扫描"; message.AppId = ConfigurationAppSetting.AppId; string data = string.Format("orderno={0}&OutTime={1}&SalesmanID={2}&ProviderID={3}&ProxyerID={4}", orderEnt.OrderNo, orderEnt.OutTime, orderEnt.SalesmanID, orderEnt.ProviderID, orderEnt.ProxyerID); message.NotifyData = data; message.MessagePriority = JinRi.Notify.Model.MessagePriorityEnum.None; message.MessageType = scanSetting.MessType; DateTime dt = DateTime.Now; bool ret = SendOrder(message, out errMsg); Double spendTime = (DateTime.Now - dt).TotalMilliseconds; Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", orderEnt.OrderNo, string.Format("订单扫描记录:发送时间【{0}】,调用耗时【{1}】ms,返回结果:【{2}】", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), spendTime, errMsg), "发送"); if (!ret) { lock (m_repeatNotifyTimesLockObj) { //进入补偿推送队列 m_repeatNotifyTimes[orderEnt.OrderNo] = 1; } _repeatNotifyPool.Write(message); } } Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", orderEnt.OrderNo, "订单扫描记录", "订单"); } } execTime.Stop(); Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", scanSetting.MessType, string.Format("扫描开始时间:【{0}】,扫描截止时间:【{1}】,扫描条数【{2}】,执行时间【{3}】ms", con.StartTime, con.EndTime, orderList.Count, execTime.ElapsedMilliseconds), ""); int sleep = (int)execTime.ElapsedMilliseconds; if (scanSetting.IdleSleepTime > sleep) { Thread.Sleep(scanSetting.IdleSleepTime - sleep); } con.StartTime = endTime.AddSeconds(scanIntervalTime); con.EndTime = GetDateTimeNow(ikey); endTime = con.EndTime; TimeSpan timeSpan = con.EndTime - DateTime.Now; double sleepTime = timeSpan.TotalMilliseconds; if (sleepTime > 0) { Process.Debug(ikey, "订单扫描记录", "GetScanMessageList", scanSetting.MessType, string.Format("休眠时间【{0}】", sleepTime.ToString(), "")); Thread.Sleep((int)sleepTime); } } } catch (Exception ex) { Process.Fatal(ikey, "订单扫描记录", "GetScanMessageList", scanSetting.MessType, string.Format("重扫线程异常结束ex【{2}】,扫描开始时间:【{0}】,扫描截止时间:【{1}】", con.StartTime.ToString("yyyy-MM-dd HH:mm:ss"), con.EndTime.ToString("yyyy-MM-dd HH:mm:ss"), ex.ToString()), ""); ThreadPool.QueueUserWorkItem(x => { try { Scan(); } catch { } } ); } }