private void StartTask() { if (_cancellationTokenSource != null) { return; //running } _cancellationTokenSource = new CancellationTokenSource(); var ct = _cancellationTokenSource.Token; _runTask = Task.Factory.StartNew(async() => { //ct.ThrowIfCancellationRequested(); while (!ct.IsCancellationRequested) { if (TASK_QUEUE.IsEmpty) { //_logger.LogInformation("task queue is empty!"); break; } ReturnT result = null; TriggerParam triggerParam = null; try { if (TASK_QUEUE.TryDequeue(out triggerParam)) { if (!ID_IN_QUEUE.TryRemove(triggerParam.LogId, out _)) { _logger.LogWarning("remove queue failed,logId={logId},jobId={jobId},exists={exists}" , triggerParam.LogId, triggerParam.JobId, ID_IN_QUEUE.ContainsKey(triggerParam.LogId)); } //set log file; _jobLogger.SetLogFile(triggerParam.LogDateTime, triggerParam.LogId); _jobLogger.Log("<br>----------- xxl-job job execute start -----------<br>----------- Param:{0}", triggerParam.ExecutorParams); result = await Executor.Execute(triggerParam, ct); _jobLogger.Log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + result.Code); } else { _logger.LogWarning("Dequeue Task Failed"); } } catch (Exception ex) { result = ReturnT.Failed("Dequeue Task Failed:" + ex.Message); _jobLogger.Log("<br>----------- JobThread Exception:" + ex.Message + "<br>----------- xxl-job job execute end(error) -----------"); } if (triggerParam != null) { CallBack?.Invoke(this, new HandleCallbackParam(triggerParam, result ?? ReturnT.FAIL)); } } _cancellationTokenSource?.Dispose(); _cancellationTokenSource = null; }, _cancellationTokenSource.Token); }