Skip to content

Blazor Server - What is the best way to create/manage a background task/thread? #51358

Answered by mahald
DavidThielen asked this question in General
Discussion options

You must be logged in to vote

@DavidThielen, you're correct about the sleep issue. My intention was to suggest implementing it within a dedicated thread.

To ensure everything operates within this thread, it's best to use the non-async functions provided by libraries. If you encounter a method that only offers an async variant, you can simply use .Wait or .Result to ensure its execution within that thread.
It's worth noting that we shouldn't use async merely because it's available. If you're certain that your task can be managed by a single thread, this approach is often more efficient. And you could set the Thread Priority if you want.

            IsBackground = true,
            Priority = ThreadPriority.BelowNormal

S…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@DavidThielen
Comment options

Comment options

You must be logged in to vote
1 reply
@DavidThielen
Comment options

Comment options

You must be logged in to vote
1 reply
@DavidThielen
Comment options

Answer selected by DavidThielen
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
3 participants