Grumson.Utilities.TaskQueue
1.0.4
See the version list below for details.
dotnet add package Grumson.Utilities.TaskQueue --version 1.0.4
NuGet\Install-Package Grumson.Utilities.TaskQueue -Version 1.0.4
<PackageReference Include="Grumson.Utilities.TaskQueue" Version="1.0.4" />
paket add Grumson.Utilities.TaskQueue --version 1.0.4
#r "nuget: Grumson.Utilities.TaskQueue, 1.0.4"
// Install Grumson.Utilities.TaskQueue as a Cake Addin #addin nuget:?package=Grumson.Utilities.TaskQueue&version=1.0.4 // Install Grumson.Utilities.TaskQueue as a Cake Tool #tool nuget:?package=Grumson.Utilities.TaskQueue&version=1.0.4
Grumson.Utilities.TaskQueue
The TaskQueue
class provides a mechanism to enqueue and process tasks with priorities. It ensures that tasks are executed based on their priority, where lower values indicate higher priority.
Features
- Rule-based preemption of tasks with lower priority by tasks with higher priority.
- Enqueue tasks with a specified priority.
- Check if a specific task is completed.
- Wait until all tasks are completed.
TaskQueue
Properties
Constructors
TaskQueue(List<PreemptionRule> preemptionRules)
: Initializes a new instance of theTaskQueue
class with the specified preemption rules.
Public Methods
Guid EnqueueTask(Func<CancellationToken, Task> task, int priority)
: Enqueues a new task with the specified priority.Task<bool> IsTaskCompletedAsync(Guid taskId)
: Checks if a task with a given ID is completed.Task AllTasksCompletedAsync()
: Waits until all tasks are completed.CancelAllTasks
: Cancels all tasks in the queue.
Private Methods
Task ProcessQueueAsync()
: Processes the task queue. This method is called internally to process tasks based on their priority. If preemption rules are defined, it preempts tasks as needed.Task<bool> PreemptTaskAsync(Guid taskId)
: Preempts a task with the specified ID. This method is called internally to preempt tasks based on preemption rules.
Usage
Initialization
To initialize a TaskQueue
, provide a list of preemption rules:
var preemptionRules = new List {
new PreemptionRule(0, 4), // Priority 0 tasks can preempt priority 4 tasks
new PreemptionRule(1, 5), // Priority 1 tasks can preempt priority 5 tasks
// Add more rules as needed
};
var taskQueue = new TaskQueue(preemptionRules);
Enqueueing Tasks
To enqueue a new task with a specified priority:
// Enqueue methods as tasks with priorities
var taskId1 = taskQueue.EnqueueTask(Method1, 4); // Lower priority
var taskId2 = taskQueue.EnqueueTask(Method2, 0); // Higher priority
Checking Task Completion
To check if a specific task is completed:
bool isCompleted = await taskQueue.IsTaskCompletedAsync(taskId);
Waiting for All Tasks to Complete
To wait until all tasks in the queue are completed:
await taskQueue.AllTasksCompletedAsync();
Cancelling All Tasks
To cancel all tasks in the queue:
taskQueue.CancelAllTasks();
Example
Here is a complete example of how to use the TaskQueue
class:
using System;
using System.Threading.Tasks;
public class Program
{
public static async Task Main(string[] args)
{
// Define preemption rules
var preemptionRules = new List<PreemptionRule>
{
new PreemptionRule(0, 4), // Priority 0 can preempt priority 4
new PreemptionRule(0, 5), // Priority 0 can preempt priority 5
// Add more rules as needed
};
var taskQueue = new TaskQueue(preemptionRules);
// Enqueue methods as tasks with priorities
var taskId1 = taskQueue.EnqueueTask(Method1, 4); // Lower priority
var taskId2 = taskQueue.EnqueueTask(Method2, 0); // Higher priority
// Wait for all tasks to complete
await taskQueue.AllTasksCompletedAsync();
Console.WriteLine("All tasks completed");
// Check if individual tasks are completed
Console.WriteLine($"Task1 completed: {await taskQueue.IsTaskCompletedAsync(taskId1)}");
Console.WriteLine($"Task2 completed: {await taskQueue.IsTaskCompletedAsync(taskId2)}");
}
public static async Task Method1(CancellationToken token)
{
// Your async task 1
for (int i = 0; i < 10; i++)
{
token.ThrowIfCancellationRequested();
await Task.Delay(100);
Console.WriteLine("Method1 running");
}
Console.WriteLine("Method1 completed");
}
public static async Task Method2(CancellationToken token)
{
// Your async task 2
for (int i = 0; i < 5; i++)
{
token.ThrowIfCancellationRequested();
await Task.Delay(100);
Console.WriteLine("Method2 running");
}
Console.WriteLine("Method2 completed");
}
}
Changelog
This section outlines the changes and improvements made in each version of the TaskQueue.
Version 1.0.4 - 2023-09-10
Fixed
- Add SemaphoreSlim to prevent race conditions in the TaskQueue.
Version 1.0.3 - 2023-09-05
- TaskQueue.cs
. Changed task completion methods to use
TrySetResult,
TrySetCanceled, and
TrySetException`.
Version 1.0.2 - 2023-09-04
New
- CanlAllTasks method to cancel all tasks in the queue.
Version 1.0.1 - 2023-09-04
Enhanced
- QueuedTask with priority, and CancellationToken.
Expanded
- Preemption features and examples.
Improved
- TaskQueue with preemption logic and priority handling.
Introduced
- PreemptionRule class for defining preemption rules.
Version 1.0.0 - 2023-09-04
- Initial release of the TaskQueue.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.