private async Task DoDequeueLoop() { IsRunning = true; while (TaskQueue.Any()) { if (CurrentTask == null) { CurrentTask = TaskQueue.Dequeue(); try { CurrentTask.StartAsync().GetAwaiter(); } catch (Exception ex) { throw new Exception("Error running queue", ex); } } else { if (!CurrentTask.IsRunning) { CurrentTask = null; } } await Task.Delay(200); } IsRunning = false; }
public static bool FileInProgress(string fileName) { try { return(TaskQueue.Any(t => t.FileName == fileName) || WorkerThreads.Any(t => t.CurrentFile == fileName)); } catch (Exception ex) { LogException(LogLevel.Warning, ex); // Just in case return(true); } }
public string GetNextTask() { //var commonds = SampleTestTask(); //JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }; //string serialized = JsonConvert.SerializeObject(commonds, settings); //return serialized; if (TaskQueue.Any()) { var commonds = TaskQueue.Dequeue(); JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }; string serialized = JsonConvert.SerializeObject(commonds, settings); return(serialized); } else { return(string.Empty); } }
private void TaskRun() { if (!isRunning) { isRunning = true; while (TaskQueue.Any()) { if (psSum >= maxPsNum) { continue; } psSum++; var taskResult = Task.Run(async () => { TaskWrapper<TaskItem> taskItem = null; try { if (TaskQueue.TryDequeue(out taskItem)) { await taskItem.Instance.TaskExcute(); //if (OnTaskResult != null) //{ // OnTaskResult(taskItem.Instance); //} } } catch (Exception exc) { var body = taskItem.Instance.WebElement.GetAttribute("innerHtml"); Console.WriteLine("Exception :========================================================================="); Console.WriteLine(body); Console.WriteLine("========================================================================="); if (taskItem.Instance.RetryTime < 3) { taskItem.Instance.RetryTime++; TaskQueue.Enqueue(taskItem); } File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "errLog.txt", taskItem.Instance.Url + "|" + string.Join("_", taskItem.Instance.State.Select(x => x.Key + "_" + x.Value).ToList())); errorNumber++; //if (taskItem != null) //{ // TaskQueue.Enqueue(taskItem); //} //UpdateProxy(); //Console.WriteLine("==========================="); //Console.WriteLine(); //Console.WriteLine("Bug:" + exc.Message); //Console.WriteLine(); //Console.WriteLine("==========================="); } }); taskResult.ContinueWith(_task => { if (_task.IsCompleted) psSum--; }); Thread.Sleep(intervalTime); Console.CursorTop = Console.WindowTop; Console.CursorLeft = 0; processNumber++; Console.WriteLine($"{processNumber}/{queueNumber} , Err: {errorNumber}"); } isRunning = false; } }