Grumson.Utilities.TaskQueue 1.0.0

There is a newer version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Grumson.Utilities.TaskQueue" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Grumson.Utilities.TaskQueue --version 1.0.0                
#r "nuget: Grumson.Utilities.TaskQueue, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 the TaskQueue 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
1.0.5 106 9/10/2024
1.0.4 88 9/10/2024
1.0.3 104 9/5/2024
1.0.2 101 9/4/2024
1.0.1 89 9/4/2024
1.0.0 94 9/4/2024