Β© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#β€’3y agoβ€’
12 replies
Jer

❔ Task tracking

Hi!

I am writing an app that relies on HostedService(s).
I've got a hosted services which has an HttpListener and whenever a new request comes in I use a Channel<T> to dispatch it to another HostedService that consumes this Channel.

This all works great!

Now, inside the
RequestQueueWorker
RequestQueueWorker
(the consumer of the Channel). I use
Task.Run()
Task.Run()
and add the Task proxy into a list.

So that in case of a graceful application shutdown I can use the
StopAsync
StopAsync
method of the
HostedService
HostedService
to
await Task.WhenAll(_runningTasks)
await Task.WhenAll(_runningTasks)
to ensure that those tasks are indeed completed.

However, ideally I'd want to clean up this list every once in a while and get rid of all tasks that are completed so that this list stays small.
Throughout the lifetime of the app there could be thousands if not more
Task
Task
's inside of this list.

Is the clean up even needed from the get go? Is it overkill? Should I even care optimizing this part considering it's only on shutdown anyway?

Thanks in advance!
I hope I provided enough information, if not feel free to ask for more details!

(Please tag me if you reply, so I get a notitication. Thanks πŸ™‚ )
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ cron task, 1 task is delaying
C#CC# / help
3y ago
βœ… First task :)
C#CC# / help
3y ago
❔ Task scheduling
C#CC# / help
3y ago
❔ Future Task
C#CC# / help
4y ago