KeyPriorityQueue 1.0.0

dotnet add package KeyPriorityQueue --version 1.0.0
                    
NuGet\Install-Package KeyPriorityQueue -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="KeyPriorityQueue" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KeyPriorityQueue" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="KeyPriorityQueue" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KeyPriorityQueue --version 1.0.0
                    
#r "nuget: KeyPriorityQueue, 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.
#:package KeyPriorityQueue@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=KeyPriorityQueue&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=KeyPriorityQueue&version=1.0.0
                    
Install as a Cake Tool

This project was pulled off from another project I had where I had to send messages to different Teams channels and each channel url would have the same delay but not shared with other channels. I would separate each channel (Key) and Queue them based on the priority that I set up in different part of the original sender.

The usage is very simple with this project:

1- Create an instance of KeyPriorityQueue<TKey, TItem> and pass the interval delay in the constructor;
2- Enqueue a task using TKey, TItem and the priority of this task, as parameters, using KeyPriorityQueue.Enqueue;
3- If the return value of Enqueue is true, it means it's a new Queue, so you can use ProcessQueue with the parameters being TKey, Func<QueueItem<TItem>, TKey, Task>. Where Func will receive the Queued item and the Key as values to be used in the callback and it will return a Task;

Usage example:

public static class Communicator
{
  private static readonly KeyPriorityQueue<string, string> priorityQueues = new();

      public static Task AddMessageToQueue(string message, string group, PriorityType priority)
      {
        bool isNew = priorityQueues.Enqueue(group, message, priority);

        if (isNew)
            Task.Run(() => priorityQueues.ProcessQueue(group, SendMessageAsync));

        return Task.CompletedTask;
      }

    private static async Task SendMessageAsync(QueueItem<string> message, string group)
    {
        if (string.IsNullOrWhiteSpace(message.Item))
        {
            GenerateConsoleMessage(204, "Empty Message");
            return;
        }

        try
        {
            StringContent content = new(message.Item, Encoding.UTF8, "application/json");

            string url = "Your URL here";
            HttpResponseMessage response = await _httpClient.PostAsync(url, content);

            if (response.StatusCode == HttpStatusCode.TooManyRequests)
            {
                await Task.Delay(_delay);
                await SendMessageAsync(message, group);
            }

            GenerateConsoleMessage(response.StatusCode, (response.IsSuccessStatusCode ? $"Ok - {await response.Content.ReadAsStringAsync()}"
                                                                                      : await response.Content.ReadAsStringAsync()));

        }
        catch (Exception ex)
        {
            GenerateConsoleMessage(500, ex.Message);
        }
    }

    private static string GenerateConsoleMessage(HttpStatusCode statusCode, string message)
        => GenerateConsoleMessage((int)statusCode, message);

    private static string GenerateConsoleMessage(int statusCode, string message)
        => $"{statusCode} - {DateTime.Now:yyyy-MM-dd HH:mm:ss.ffff} - {message}";

}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.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.0 257 4/17/2025