Consider having an application which creates 30 app-domains, then runs them (each app-domain in its own thread) and when each of these app-domains finishes running (aka its thread exits and so on) we need to cleanup by running for each appdomain some custom cleanup-logic + a call for unloading the appdomain itself.
The cleanup logic + appdomain-unloading call for each of these app-domains might require more than one attempts to succeed (due to resources being involved, taking time to be released by the system and so on). If the cleanup operation cannot be performed in a specific attempt, it doesn't take more than 100ms for us to know about this and move on.
What is the best practice, in the world of C#, to perform such cleanup in a 'perform-cleanup-in-the-background' fashion? Possible venues off the top of my head:
Each app-domain cleanup should be performed in its very own 'new Thread()' corner. Each thread persists in a while loop with a sleep interval in case it needs to retry.
Have just a single, dedicated long-running thread with a task-queue in which we submit each and every appdomain to be cleaned-up and unloaded (again in a persistent fashion like in method #1 above).
Using a thread-pool and submitting the cleanup-tasks there
According to the following comment:
https://stackoverflow.com/a/28651533/863651
"If a method cannot be expected to exit within 100ms or so of when it starts execution, the method should be executed via some means other than the main thread pool.[ ... ] If, however, a method will take a second or longer to execute, and will spend most of its time blocked, the method should likely be run in a dedicated thread, and should almost certainly not be run in a main-threadpool thread."
I guess this discourages using method #3 above. I'm wondering if method#2 has any considerable advantages over method #1. The main thing that bugs me is that even though method#2 needs slightly more coding it uses just 1 thread no matter what, while method#2 will require N threads for N app-domains (with all the cost that this entails in terms of spawning the threads etc).
I'm open to suggestions about any method#4+ that there might be to implement such mechanism. I'm just curious to see how other programmers apply the concept of "best threading-practices" when it comes to such a problem.
Thanks in advance.
P.S.: This application is meant to be run in contemporary desktop computers (at the time of this writing).