Why I still start it in .NET
A consolidation job went from about five minutes to sixteen seconds inside the runtime that was already running the plants. More threads was the second edit.
People ask why the new service is still .NET. The honest answer is dull, which is why I trust it. Energy Studio Pro already runs on .NET, including libraries that are still on the Framework side of a slow move to current .NET. A plant does not get a freeze window so I can change runtimes. When a job is late, I want the thread pool, a profiler, and a cancellation token in the same process that is already in production.
The number I can stand behind: one representative consolidation job went from about five minutes to sixteen seconds. Same business result, checked. Event prioritisation on the same 5,371 events went from about 7,783 ms to about 804 ms. Those are two different fixes. Neither of them was "C# is fast".
The first edit was the work
Consolidation is the heavy pass behind monitoring and reports. It was sequential, and a lot of the calculation was repeated per item. Parallelism on top of repeated work makes the repetition faster. It does not make it smaller. I removed the repeated calculation first, then let the rest run concurrently, and compared the output to the old run.
The prioritiser had the other shape. The cost grew with the length of the event list, and most of that cost was unused. Cutting the unused work, and changing how rules were evaluated, is what moved 7,783 ms to 804 ms. A bigger thread pool would have burned CPU on the same waste.
That is the part I care about when someone says they rewrote a hot path in a faster language. Show me the profile. If the top of it is work the caller does not need, the language is not in the room yet.
The pool is the constraint
Once the work is real, .NET's thread pool is the architecture, not a setting. It injects threads slowly on purpose. A burst of blocked calls looks like a hang, because the pool will not mint a thread per request to save you. The usual way to get there is sync-over-async: .Result, .Wait, or a lock held across a database call. The thread sits there for the whole wait. Under load the pool starves, and the symptom shows up somewhere else, often as timeouts in a service that did nothing wrong.
Async is how you give the thread back while SQL Server is thinking. It is not a speedup by itself. Parallel.ForEachAsync is how I then cap how many of those operations exist at once. The cap is a promise to the database, not a guess at the core count. Each in-flight item can hold a connection. Set the degree to the number of commands the server can absorb, and pass the cancellation token through so a cancelled job releases them.
await Parallel.ForEachAsync(
batch,
new ParallelOptions
{
MaxDegreeOfParallelism = maxInFlight,
CancellationToken = stoppingToken,
},
async (item, token) =>
{
token.ThrowIfCancellationRequested();
await ConsolidateAsync(item, token);
});If the body blocks, that degree becomes "pool threads stuck". If the body is actually async, it becomes "operations in flight", which is the number SQL Server feels. I have watched people tune the first and measure the second.
A latency budget is a different queue
Throughput work can wait. An alarm cannot. In the scenarios we measured, a plant event is on the dashboard in under 200 ms. An unbounded queue hides the moment you miss that budget: the producer keeps allocating, the consumer falls behind, and the dashboard is late while the process looks busy.
When the requirement is a latency, I use a bounded channel and I pick the full mode in the open. Wait pushes back on the producer. Drop means an event does not get through, which for an alarm is a product decision and has to be one I can explain. I will not bury either choice in a ConcurrentQueue that grows until the GC does it for me.
var incoming = Channel.CreateBounded<PlantEvent>(
new BoundedChannelOptions(capacity)
{
FullMode = BoundedChannelFullMode.Wait,
SingleReader = true,
SingleWriter = false,
});This is the shape I want, not a claim that one channel produced the 200 ms. The measured path is still processing, subscriptions, and delivery across a system that includes old OPC DA. The channel is how I keep a new stage from lying about its backlog.
What .NET is for, here
I start the long-running service in .NET when it has to live next to the code that is already up, talk to SQL Server, and still be debuggable in two years. The pool, the GC, Channels, and a cancellation story that runs from a button to a command are the features. The syntax is a convenience.
A page is a different job. I don't have this argument there. The argument I do have, on this product, is whether the next edit removes work or only schedules it. I measure one change against the previous run. Sixteen seconds is a result on that job, not a property of the language.