Category: .NET Core

  • Stop Abusing Queues: System.Threading.Channels in .NET

    Stop Abusing Queues: System.Threading.Channels in .NET

    Every .NET codebase I’ve worked on eventually grows a “background work” problem. A request comes in, something slow needs to happen as a result, and nobody wants the user staring at a spinner while it does. So someone reaches for Task.Run and forgets about it. Then someone else wraps a ConcurrentQueue<T> in a while (true) loop with a Thread.Sleep(100). Then a third person adds a lock because two consumers started fighting.

    None of that is necessary. .NET has had a purpose-built answer since Core 3.0, and it lives in a namespace most people have never opened: System.Threading.Channels.

    (more…)