/// <summary> /// 定时器每次跑起判断是否要确定执行ExecMethod /// </summary> public void TimerExec(RichTextConsole console, Action <AbstractTimerTask> refreshAction) { DateTime nowTime = DateTime.Now; TimeSpan intervalTimeSpan = nowTime - lastExecTime; if (intervalTimeSpan.TotalSeconds > IntervalTime && this.ExecStatus == ExecStatus.WaitExec) { this.ExecStatus = ExecStatus.Working; refreshAction(this); ThreadPool.QueueUserWorkItem(state => { try { lastExecTime = nowTime; ExecMethod(console); } catch (Exception ex) { console.Error("检测到【" + TimerName + "】定时任务,有未捕获的异常", ex); } finally { this.ExecStatus = ExecStatus.WaitExec; refreshAction(this); } }); } }
/// <summary> /// 执行定时任务,保存日志到库 /// </summary> /// <param name="richTextLog">winform日志显示</param> public override void ExecMethod(RichTextConsole console) { console.Debug($"{TimerName}定时任务,开始执行...", false); int row = logManage.LogForRedisToDB(); console.Info($"{TimerName}定时任务,已执行完成,影响行数:{row}。", isWriteLog: false); }
public TimerControl() { InitializeComponent(); console = new RichTextConsole(ShowText); timerTaskList = new List <AbstractTimerTask>(); AppendTimerTask(); timerTimeTask.Enabled = true; RenderTimerTask(); }
/// <summary> /// 定时任务执行的方法 /// </summary> /// <param name="console">控制台输出</param> public override void ExecMethod(RichTextConsole console) { console.Info($"执行{TimerName}定时任务开始...", isWriteLog: false); totalSearch++; if (totalSearch > 50)//总共查找次数达到50重新从第一页开始 { totalSearch = 1; pageIndex = 1; } JuHeResponse <JokesData> jokeResult = jokeProvider.SearchNewestJokes(pageIndex); if (jokeResult.error_code == 0) { List <JokeInfo> jokeList = jokeResult.result.data; List <JokeInfoDto> jokeDtoList = jokeList.Select(joke => new JokeInfoDto { Id = joke.hashId, Content = joke.content, CreateTime = TimeHelper.GetDateTime(joke.unixtime), UpdateTime = joke.updatetime }).ToList(); int saveRow = jokeInfoManage.SaveRangeJokes(jokeDtoList); if (saveRow > 0) { console.Info($"当前页码:{pageIndex},保存最新笑话数据成功,共保存{saveRow}条记录", isWriteLog: false); if (pageIndex > 1) { pageIndex++; } } else { console.Warn($"当前页码:{pageIndex},保存最新笑话数据0条,可能远程没有更新笑话", isWriteLog: false); nonDataIndex++; if (nonDataIndex > 2) { pageIndex = pageIndex * 2; nonDataIndex = 0;//快速双倍翻页后,累计没有保存到数据的次数清0 } else { pageIndex++; } } } else { console.Error($"定时获取最新笑话数据储存:error_code ={jokeResult.error_code},reason ={jokeResult.reason}"); } console.Info($"执行{TimerName}定时任务结束", isWriteLog: false); }
/*private OvermindClientTab GetClientTab(NetPacket packet) * { * //return (OvermindClientTab)this.tabControl.TabPages[this._server.GetReceiverName(packet)]; * OvermindClientTab tab = (OvermindClientTab)this.tabControl.TabPages["#" + packet.Header.Source.ToString()]; * return tab; * }*/ private void UpdateTerminal(RichTextConsole console, Color c, object obj) { try { if (obj is NetEventArgs) { NetEventArgs e = obj as NetEventArgs; if (e.Message != null) { console.TerminalAppendLine(e.Message, c); } } else if (obj is String) { console.TerminalAppendLine(obj as string, c); } } catch (Exception) { } }
/// <summary> /// 定时任务执行的方法 /// </summary> /// <param name="console">控制台输出</param> public virtual void ExecMethod(RichTextConsole console) { console.Info("执行了定时任务执行的默认方法"); }
/// <summary> /// 执行定时任务,保存日志到库 /// </summary> /// <param name="richTextLog">winform日志显示</param> public override void ExecMethod(RichTextConsole console) { console.Debug($"{TimerName}定时任务,开始执行...", false); List <AreaCodeName> areaCodeNameList = areaProvider.SearchAreaCode(AreaLevel.Province); List <WeatherCity> weatherCities = new List <WeatherCity>(); areaCodeNameList.ForEach(areaCodeName => { WeatherCity weatherCity = new WeatherCity() { Id = areaCodeName.Code, Name = areaCodeName.Name, ParentId = "null", WCityLevel = WCityLevel.PROVINCE, WCityLevelName = WCityLevel.GetName(WCityLevel.PROVINCE), CreateTime = DateTime.Now }; weatherCities.Add(weatherCity); }); weatherCityManage.SaveWeatherCity(WinFormConfig.CoreDBConnectString, weatherCities); console.Info($"{TimerName}定时任务,成功保存省份集合信息。", isWriteLog: false); weatherCities.Clear();//清空省份数据,开始保存城市数据 areaCodeNameList.ForEach(areaCodeName => { List <AreaCodeName> cityAreaCodeNameList = areaProvider.SearchAreaCode(AreaLevel.City, areaCodeName.Code); for (int i = 0; i < cityAreaCodeNameList.Count; i++) { var cityAreaCodeName = cityAreaCodeNameList[i]; WeatherCity weatherCity = new WeatherCity() { Id = cityAreaCodeName.Code, Name = cityAreaCodeName.Name, ParentId = areaCodeName.Code, WCityLevel = WCityLevel.CITY, WCityLevelName = WCityLevel.GetName(WCityLevel.CITY), CreateTime = DateTime.Now }; weatherCities.Add(weatherCity); } weatherCityManage.SaveWeatherCity(WinFormConfig.CoreDBConnectString, weatherCities); console.Info($"{TimerName}定时任务,成功保存{areaCodeName.Name}省份的城市集合信息。", isWriteLog: false); weatherCities.Clear();//清空城市数据,开始保存城市的区域数据 cityAreaCodeNameList.ForEach(cityCodeName => { List <AreaCodeName> regionCodeNameList = areaProvider.SearchAreaCode(AreaLevel.Region, cityCodeName.Code); foreach (var regionCodeName in regionCodeNameList) { WeatherCity weatherCity = new WeatherCity() { Id = regionCodeName.Code, Name = regionCodeName.Name, ParentId = cityCodeName.Code, WCityLevel = WCityLevel.REGION, WCityLevelName = WCityLevel.GetName(WCityLevel.REGION), CreateTime = DateTime.Now }; weatherCities.Add(weatherCity); } weatherCityManage.SaveWeatherCity(WinFormConfig.CoreDBConnectString, weatherCities); console.Info($"{TimerName}定时任务,成功保存{cityCodeName.Name}城市的区域集合信息。", isWriteLog: false); weatherCities.Clear(); //清空区域数据,开始保存下一个城市的区域数据 }); weatherCities.Clear(); //清空城市数据,开始保存下一个省份的城市数据 }); console.Info($"{TimerName}定时任务,已执行完成。", isWriteLog: false); }