public MainPage() { m_lpActual = ((App)Application.Current).LpCurrent(); m_lp = new UpcLogProvider(m_lpActual); InitializeComponent(); m_ups = new Scanner(Dispatcher, scannerControl); m_upca = new UpcAlert(); m_sb = new StatusBox(); m_upccCore = new UpcInvCore(m_upca, m_sb, m_lp); m_plsProcessing = new List <string>(); m_sb.Initialize(recStatus, m_upca); AdjustUIForMediaType(m_adasCurrent, m_wdiCurrent); AdjustUIForAvailableHardware(); m_board = new WorkBoard(UpdateWorkBoard); m_pipeline = new ProducerConsumer <Transaction>(null, TransactionDispatcher); m_pipeline.Start(); }
private void btn_Satrt_Click(object sender, EventArgs e) { if (!_noticeFlag) { _noticeFlag = true; _noticeDateTime = DateTime.Now; Logging("启动[提醒]服务"); SetbtnState(btn_Satrt, "停止[提醒]服务", true); SetlblState(lbl_State, "[提醒]扫描服务运行中(" + _noticeDateTime + ")", Color.Green); //重置提醒扫描状态:正在扫描--> 等到扫描 var resetStandbyTask = new Task <bool>(() => { var isReset = false; try { isReset = ResetAllRemindScan(); } catch { } return(isReset); }); resetStandbyTask.Start(); resetStandbyTask.Wait(); var isSuccess = resetStandbyTask.Result; if (isSuccess) { Logging("重置\"扫描中\"的[提醒]扫描服务为等待扫描成功!"); } else { Logging("[提醒]扫描服务启动失败:重置\"扫描中\"的[提醒]扫描服务为等待扫描失败!"); _noticeFlag = false; SetbtnState(btn_Satrt, "启动[提醒]扫描服务", true); SetlblState(lbl_State, "[提醒]扫描服务停止", Color.Red); return; } //初始化相关变量、工作线程 //等到重置“发送中”任务结束后,才能执行下面任务: //获取[未提醒]列表数据,为[发起提醒]生产者提供生产方法 var getRetryTimes = 0; Func <List <string> > ProduceFunc = () => { //休眠获取[提醒]列表数据,主要为了间断查询。 Thread.Sleep(_SleepQuery); var unList = new List <string>(); try { unList = GetTaskList(); if (unList.Count > 0) { Logging("获取[提醒]队列成功!"); } getRetryTimes = 0; } catch (Exception ex) { getRetryTimes = getRetryTimes + 1; if (getRetryTimes >= _GetQueryRetryTimes) { _noticeFlag = false; Logging( "[提醒]服务意外停止:获取[未提醒]队列连续失败" + getRetryTimes + "次!|| 错误原因:" + (ex.InnerException == null ? ex.Message : ex.InnerException.ToString()), (string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); ChangeAllServerState(); if (_producerConsumer != null) { _producerConsumer.Stop(); } } else { Logging( "获取[未提醒]队列连续失败" + getRetryTimes + "次!|| 错误原因:" + (ex.InnerException == null ? ex.Message : ex.InnerException.ToString()), (string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); } } return(unList); }; int consumeRetryTimes = 0; //为[未提醒]消费者提供消费方法 Action <string> notNoticeAction = item => { if (!string.IsNullOrEmpty(item)) { long durTime = -1; var durTimeUnit = "毫秒"; var durTimer = new Stopwatch(); durTimer.Start(); try { //执行活动提交服务 var result = PostRemind(item); durTimer.Stop(); durTime = durTimer.ElapsedMilliseconds; if (durTime >= 1000) { durTimeUnit = "秒"; durTime = durTime / 1000; } lock (_noticeLock) { consumeRetryTimes = 0; } Logging("[未提醒]提交成功." + "(商家ID:" + item + ",运行结果:" + (result ? "提醒成功" : "提醒失败") + ",耗时:" + durTime.ToString() + durTimeUnit + ")"); } catch (Exception ex) { lock (_noticeLock) { consumeRetryTimes++; if (consumeRetryTimes >= _GetQueryRetryTimes) { _noticeFlag = false; Logging( "[未提醒]服务意外停止:提交提醒服务连续失败" + consumeRetryTimes + "次![其中包含ID:" + item + "] || 错误原因:" + (ex.InnerException == null ? ex.Message : ex.InnerException.ToString()), (string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); ChangeAllServerState(); if (_producerConsumer != null) { _producerConsumer.Stop(); } } else { Logging( "[未提醒]提交提醒服务连续失败" + consumeRetryTimes + "次![其中包含商家ID:" + item + "] || 错误原因:" + (ex.InnerException == null ? ex.Message : ex.InnerException.ToString()), (string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); } } } Thread.Sleep(_SleepPost); } }; //构造生产消费模式,初始化如生产方法,消费方法,指定消费者数量 _producerConsumer = new ProducerConsumer <string>(ProduceFunc, notNoticeAction, _remainStandbyCount, _reminTaskNum); _producerConsumer.Start(); } else //停止 { _noticeFlag = false; if (_producerConsumer != null) { _producerConsumer.Stop(); } Logging("手动停止[未提醒]扫描服务"); SetbtnState(btn_Satrt, "启动[提醒]扫描服务", true); SetlblState(lbl_State, "[提醒]扫描服务停止", Color.Red); } }
private void btn_Start_Click(object sender, EventArgs e) { if (!_noticeFlag) { _noticeFlag = true; _noticeDateTime = DateTime.Now; Logging("启动[OSS更新]服务"); SetbtnState(btn_Start, "停止[OSS更新]服务", true); SetlblState(lbl_text, "[OSS更新]扫描服务运行中(" + _noticeDateTime + ")", Color.Green); //重置提醒扫描状态:正在扫描--> 等到扫描 var resetStandbyTask = new Task<bool>(() => { var isReset = false; try { isReset = ResetAllRemindScan(); } catch { } return isReset; }); resetStandbyTask.Start(); resetStandbyTask.Wait(); var isSuccess = resetStandbyTask.Result; if (isSuccess) { Logging("重置\"扫描中\"的[OSS更新]扫描服务为等待扫描成功!"); } else { Logging("[OSS更新]扫描服务启动失败:重置\"扫描中\"的[OSS更新]扫描服务为等待扫描失败!"); _noticeFlag = false; SetbtnState(btn_Start, "启动[OSS更新]扫描服务", true); SetlblState(lbl_text, "[OSS更新]扫描服务停止", Color.Red); return; } //初始化相关变量、工作线程 //等到重置“发送中”任务结束后,才能执行下面任务: //获取[未提醒]列表数据,为[发起提醒]生产者提供生产方法 var getRetryTimes = 0; Func<List<string>> ProduceFunc = () => { //休眠获取[提醒]列表数据,主要为了间断查询。 Thread.Sleep(_sleepQuery); var unList = new List<string>(); try { unList = GetTaskList(); if (unList.Count > 0) { Logging("获取[OSS更新]队列成功!"); } getRetryTimes = 0; } catch (Exception ex) { getRetryTimes = getRetryTimes + 1; if (getRetryTimes >= _getQueryRetryTimes) { _noticeFlag = false; Logging("[OSS更新]服务意外停止:获取[未更新]队列连续失败" + getRetryTimes + "次!|| 错误原因:" +(ex.InnerException == null ? ex.Message : ex.InnerException.ToString()),(string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); ChangeAllServerState(); if (_producerConsumer != null) { _producerConsumer.Stop(); } } else { Logging("获取[未更新]队列连续失败" + getRetryTimes + "次!|| 错误原因:" +(ex.InnerException == null ? ex.Message : ex.InnerException.ToString()),(string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); } } return unList; }; int consumeRetryTimes = 0; //为[未提醒]消费者提供消费方法 Action<string> notNoticeAction = item => { if (!string.IsNullOrEmpty(item)) { long durTime = -1; var durTimeUnit = "毫秒"; var durTimer = new Stopwatch(); durTimer.Start(); try { //执行活动提交服务 var result = PostRemind(item); durTimer.Stop(); durTime = durTimer.ElapsedMilliseconds; if (durTime >= 1000) { durTimeUnit = "秒"; durTime = durTime / 1000; } lock (_noticeLock) { consumeRetryTimes = 0; } Logging("[未更新]提交成功." + "(数据ID:" + item + ",运行结果:" + (result ? "提醒成功" : "提醒失败") + ",耗时:" +durTime.ToString() + durTimeUnit + ")"); } catch (Exception ex) { lock (_noticeLock) { consumeRetryTimes++; if (consumeRetryTimes >= _getQueryRetryTimes) { _noticeFlag = false; Logging("[未更新]服务意外停止:提交提醒服务连续失败" + consumeRetryTimes + "次![其中包含ID:" + item +"] || 错误原因:" +(ex.InnerException == null ? ex.Message : ex.InnerException.ToString()),(string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); ChangeAllServerState(); if (_producerConsumer != null) { _producerConsumer.Stop(); } } else { Logging("[未更新]提交提醒服务连续失败" + consumeRetryTimes + "次![其中包含商家ID:" + item +"] || 错误原因:" +(ex.InnerException == null ? ex.Message : ex.InnerException.ToString()),(string.IsNullOrWhiteSpace(ex.StackTrace) ? "" : ex.StackTrace)); } } } Thread.Sleep(_sleepPost); } }; //构造生产消费模式,初始化如生产方法,消费方法,指定消费者数量 _producerConsumer = new ProducerConsumer<string>(ProduceFunc,notNoticeAction, _remainStandbyCount,_reminTaskNum); _producerConsumer.Start(); } else //停止 { _noticeFlag = false; if (_producerConsumer != null) { _producerConsumer.Stop(); } Logging("手动停止[未更新]扫描服务"); SetbtnState(btn_Start, "启动[OSS更新]扫描服务", true); SetlblState(lbl_text, "[OSS更新]扫描服务停止", Color.Red); } }
private void DoCreatePipeline(object sender, EventArgs e) { Log($"{DateTime.Now}: Creating Pipeline"); pipeline = new ProducerConsumer <WorkItem>(null, ProcessWorkItems); pipeline.Start(); }
static void Main() { var manager = new ProducerConsumer(); manager.Start(); }