/// <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); }
/// <summary> /// 修改选中的定时任务的状态 /// </summary> /// <param name="execStatus">执行状态</param> private void ChangeTimerTaskStatus(ExecStatus execStatus) { string changeTxt = "开启"; if (execStatus == ExecStatus.PausedExec) { changeTxt = "暂停"; } if (timerDataGridView.SelectedRows.Count == 0) { MessageBox.Show(timerDataGridView, $"请选择要{changeTxt}的定时任务", "定时任务提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } for (int i = 0; i < timerDataGridView.SelectedRows.Count; i++) { DataGridViewRow row = timerDataGridView.SelectedRows[i]; int index = timerDataGridView.Rows.IndexOf(row); if (index >= timerTaskList.Count) { MessageBox.Show(this, "请选择有效的定时任务", "定时任务提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); continue; } AbstractTimerTask timerTask = timerTaskList[index]; if (timerTask.ExecStatus == ExecStatus.Working) { console.Warn($"请等到【{timerTask.TimerName}】执行完成,再{changeTxt}。"); continue; } else if (timerTask.ExecStatus == ExecStatus.TerminatedExec) { console.Warn($"【{timerTask.TimerName}】已终止执行,无法{changeTxt}。"); continue; } timerTask.ExecStatus = execStatus; RefreshTimerTask(timerTask); } }