Skip to main content

Command Palette

Search for a command to run...

.NET 9 prioritised Channel

Updated
2 min read
.NET 9 prioritised Channel
P
Senior Software Engineer specialising in cloud architecture, distributed systems, and modern .NET development, with over two decades of experience designing and delivering enterprise platforms in financial, insurance, and high-scale commercial environments. My focus is on building systems that are reliable, scalable, and maintainable over the long term. I’ve led modernisation initiatives moving legacy platforms to cloud-native Azure architectures, designed high-throughput streaming solutions to eliminate performance bottlenecks, and implemented secure microservices environments using container-based deployment models and event-driven integration patterns. From an architecture perspective, I have strong practical experience applying approaches such as Vertical Slice Architecture, Domain-Driven Design, Clean Architecture, and Hexagonal Architecture. I’m particularly interested in modular system design that balances delivery speed with long-term sustainability, and I enjoy solving complex problems involving distributed workflows, performance optimisation, and system reliability. I enjoy mentoring engineers, contributing to architectural decisions, and helping teams simplify complex systems into clear, maintainable designs. I’m always open to connecting with other engineers, architects, and technology leaders working on modern cloud and distributed system challenges.

.NET 9 introduces CreateUnboundedPrioritized in System.Threading.Channels!

Unlike FIFO channels, it retrieves the highest priority element first, using Comparer<T>.Default or a custom IComparer<T>.

This new method allows for more flexible and efficient message processing by enabling priority based dequeuing. Instead of handling elements in a strict first in, first out (FIFO) order, CreateUnboundedPrioritized ensures that higher priority items are always dequeued first. This makes it particularly useful for scenarios like task scheduling, event processing, or job queues where certain operations need to be handled with greater urgency. Developers can leverage IComparer<T> to define custom priority rules, allowing for fine grained control over execution order.

Example

using System;
using System.Collections.Generic;
using System.Threading.Channels;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        // Create an unbounded prioritized channel using a custom comparer (higher numbers have higher priority)
        var channel = Channel.CreateUnboundedPrioritized<int>(new PriorityComparer());

        // Writer task to enqueue items with different priorities
        var writer = Task.Run(async () =>
        {
            await channel.Writer.WriteAsync(5);  // Medium priority
            await channel.Writer.WriteAsync(1);  // Low priority
            await channel.Writer.WriteAsync(10); // High priority
            await channel.Writer.WriteAsync(7);  // Higher than 5, lower than 10

            channel.Writer.Complete(); // Signal completion
        });

        // Reader task to dequeue items based on priority
        var reader = Task.Run(async () =>
        {
            await foreach (var item in channel.Reader.ReadAllAsync())
            {
                Console.WriteLine($"Dequeued: {item}");
            }
        });

        await Task.WhenAll(writer, reader);
    }

    // Custom comparer: higher values have higher priority
    class PriorityComparer : IComparer<int>
    {
        public int Compare(int x, int y) => y.CompareTo(x); // Reverse order for priority
    }
}
17 views