Grumson.Utilities.TaskQueue
1.0.0
See the version list below for details.
dotnet add package Grumson.Utilities.TaskQueue --version 1.0.0
NuGet\Install-Package Grumson.Utilities.TaskQueue -Version 1.0.0
<PackageReference Include="Grumson.Utilities.TaskQueue" Version="1.0.0" />
paket add Grumson.Utilities.TaskQueue --version 1.0.0
#r "nuget: Grumson.Utilities.TaskQueue, 1.0.0"
// Install Grumson.Utilities.TaskQueue as a Cake Addin #addin nuget:?package=Grumson.Utilities.TaskQueue&version=1.0.0 // Install Grumson.Utilities.TaskQueue as a Cake Tool #tool nuget:?package=Grumson.Utilities.TaskQueue&version=1.0.0
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
- Enqueue tasks with a specified priority.
- Check if a specific task is completed.
- Wait until all tasks are completed.
Usage
Initialization
To create an instance of the TaskQueue
class:
var taskQueue = new TaskQueue();
Enqueueing Tasks
To enqueue a new task with a given priority:
taskQueue.Enqueue(() => Console.WriteLine("Task 1"), 1);
taskQueue.Enqueue(() => Console.WriteLine("Task 2"), 2);
taskQueue.Enqueue(() => Console.WriteLine("Task 3"), 3);
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 are completed:
await taskQueue.WaitForAllTasksAsync();
Code Overview
Properties
_tasks
: A priority queue to hold tasks with their priorities._signal
: A semaphore to signal the presence of new tasks._taskCompletionSources
: A dictionary to keep track of task completion sources by their IDs._allTasksCompleted
: A task completion source to signal when all tasks are completed.
Constructors
TaskQueue()
: Initializes a new instance of theTaskQueue
class and starts processing the queue.
Public Methods
Guid EnqueueTask(Func<Task> task, int priority)
: Enqueues a new task with a given priority and returns the unique identifier of the enqueued task.Task<bool> IsTaskCompletedAsync(Guid taskId)
: Checks if a task with a given ID is completed.Task AllTasksCompletedAsync()
: Waits until all tasks are completed.
Private Methods
Task ProcessQueue()
: Processes the task queue continuously, executing tasks based on their priority.
Example
Here is a complete example of how to use the TaskQueue
class:
using System;
using System.Threading.Tasks;
namespace TaskQueueExample
{
class Program
{
static async Task Main(string[] args)
{
var taskQueue = new TaskQueue();
Guid taskId1 = taskQueue.EnqueueTask(async () => { await Task.Delay(1000); // Simulate work Console.WriteLine("Task 1 completed"); }, priority: 1);
Guid taskId2 = taskQueue.EnqueueTask(async () => { await Task.Delay(500); // Simulate work Console.WriteLine("Task 2 completed"); }, priority: 0);
bool isTask1Completed = await taskQueue.IsTaskCompletedAsync(taskId1);
bool isTask2Completed = await taskQueue.IsTaskCompletedAsync(taskId2);
await taskQueue.AllTasksCompletedAsync();
Console.WriteLine("All tasks completed");
}
}
}
Changelog
This section outlines the changes and improvements made in each version of the TaskQueue.
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.