private async Task _executeTask(TaskData taskData) { if (!taskData.ExecuteImmediately) { await Task.Delay(_options.Value.Interval); } await Handler.Handle(taskData); Interlocked.Decrement(ref _threadCount); if (PoolThreadManager != null) { await PoolThreadManager.Release(); } }
public virtual async Task Start() { var canStart = false; lock (_lock) { if (!Running) { Running = true; canStart = true; } } if (canStart) { while (Running) { TaskData data = null; lock (_lock) { //todo: the sort of queue data is not stable for now. if (_threadCount < _options.Value.MaxThreads && TryDequeue(out data)) { if (PoolThreadManager == null || PoolThreadManager.Lock().ConfigureAwait(false) .GetAwaiter().GetResult()) { Interlocked.Increment(ref _threadCount); } else { Enqueue(data); } } } if (data != null) { // Execute asynchronously. _executeTask(data); } else { // Avoid unnecessary cpu usage. await Task.Delay(1); } } } }
private async void _executeTask(TaskQueue task, TaskData taskData) { if (!taskData.ExecuteImmediately) { await Task.Delay(Math.Max(_options.MinInterval, task.Options.Interval)); } var newTaskData = await task.ExecuteAsync(taskData); if (_status == TaskQueuePoolStatus.Running && newTaskData?.Any() == true) { foreach (var d in newTaskData) { Enqueue(d); } } Interlocked.Decrement(ref _currentThreadCount); Interlocked.Decrement(ref task.CurrentThreadCount); }
public void Enqueue(TaskData taskData) { Interlocked.Increment(ref _queuedTaskDataCount); _queuedTaskData.Enqueue(taskData); }
public abstract bool OnException(TaskData taskData, Exception e);
public abstract bool MatchedTaskData(TaskData taskData);
public abstract Task <List <TaskData> > ExecuteAsync(TaskData taskData);
public virtual bool CanHandle(TaskData data) { return(data is TTaskData); }
protected virtual Task OnException(TaskData data, Exception e) => Task.CompletedTask;