From 9e8e0786a231d42f51ae33b6225b146d09288838 Mon Sep 17 00:00:00 2001 From: srnwmnn <33450835+srnwmnn@users.noreply.github.com> Date: Thu, 19 Feb 2026 19:20:43 +0100 Subject: [PATCH] create task using TaskCreationOptions.LongRunning to prevent thread pool starvation --- .../src/BackgroundService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs index 7eb877cba2ec03..ea2ca4b5f0e7ad 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs @@ -43,7 +43,7 @@ public virtual Task StartAsync(CancellationToken cancellationToken) _stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); // Execute all of ExecuteAsync asynchronously, and store the task we're executing so that we can wait for it later. - _executeTask = Task.Run(() => ExecuteAsync(_stoppingCts.Token), _stoppingCts.Token); + _executeTask = Task.Factory.StartNew(() => ExecuteAsync(_stoppingCts.Token), _stoppingCts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); // Always return a completed task. Any result from ExecuteAsync will be handled by the Host. return Task.CompletedTask;