Skip to content

Add synchronous long running scenario. #33345

@Atomosk

Description

@Atomosk

Description

What will happen if ExecuteAsync is a synchronous and is a really long running, like several hours?

StartAsync will hang for a long time and the rest of that application will wait for that quote background service to finish what it does.

How can we avoid this? Should we use Task.Run or Task.Factory.StartNew with a LongRunning parameter or add await Task.Delay(1) to the first line of ExecuteAsync or Thread + TaskCompletitionSource or something else? Searching for an answer to this question is harder than it might look.

So I think this article is a perfect place to show a solution to that problem, which is very common I believe.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-9.0&tabs=visual-studio

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/host/hosted-services.md

Document ID

b2dece1f-15b8-03af-6d52-7cc070432b4b

Article author

@tdykstra


Associated WorkItem - 345246

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions