Note that there are some explanatory texts on larger screens.

plurals
  1. POShutting down a multithreaded application
    text
    copied!<p>I'm trying to write a ThreadManager for my C# application. I create several threads:<br> One thread for my text writer.<br> One thread that monitors some statistics.<br> Multiple threads to perform a large sequence of calculations (up to 4 threads per core and I run my app on a 2x quad core server).</p> <p>My application normally runs for up to 24 hours at a time, so all the threads get created in the beginning and they persist through the entire time the app runs.</p> <p>I want to have a single place where I "register" all of my treads and when the application is shutting down I simply invoke a method and it goes through all of the registered threads and shuts them down.</p> <p>For that purpose I have devised the following class: </p> <pre><code>public class ThreadManager { private static Object _sync = new Object(); private static ThreadManager _instance = null; private static List&lt;Thread&gt; _threads; private ThreadManager() { _threads = new List&lt;Thread&gt;(); } public static ThreadManager Instance { get { lock (_sync) { if (_instance == null) { _instance = new ThreadManager(); } } return _instance; } } public void AddThread(Thread t) { lock (_sync) { _threads.Add(t); } } public void Shutdown() { lock (_sync) { foreach (Thread t in _threads) { t.Abort(); // does this also abort threads that are currently blocking? } } } } </code></pre> <p>I want to ensure that all of my threads are killed so the application can close properly and shutting down in the middle of some computation is just fine too. Should I be aware of anything here? Is this approach good given my situation?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload