title: How to release a non responsive thread in c#
body: <p>I have implemented an application which is creating new threads and performing operations on that.
  I was always thinking if some how a thread hang or unresponsive then how to release allocated memory and other resources used by that thread.</p>
  
  <p>Please note in my application, I have already resolved following things. So i want some expert level answers more specific to killing or releasing thread.</p>
  
  <pre><code>1. Creating thread using Thread class
  2. 3-synchronization blocks (using lock), minimal number of blocks
  3. Thread safe members
  4. carefully handled exceptions
  </code></pre>
  
  <p>After reading few expert's articles and implementing some solutions , i came to know that ,
  Thread.abort() should not use in most of the cases.It should be used only if we know where and what state or what kind of resources is currently using by that thread.
  For me it's looks like a goal that can't be archived.</p>
  
  <p>Because we never know what/where the thread actually hanging on. whether it is acquired a lock at the movement or it is reading/writing to a file.</p>
  
  <p>So if we kill/abort a thread in any of those cases, it can make the situation worse. 
  So Non of other threads will be able to get any of those resources has been used by that thread and application will stop working or crash.</p>
  
  <p>It is Surprise to see lot of people came-up with their own solution , even-though there are much dangerous factors that need to be coincided.</p>
  
  <p>I believe that we cannot use thread.abort() (because of above reasons). What we can do is preventing or giving less chance for making unresponsive thread by ,</p>
  
  <pre><code>   1. reducing number of synchronization blocks
     2. proper I/O handling 
     3. proper exception handling 
     4. reducing piece of code inside sync blocks
  </code></pre>
  
  <p>After implementing above things also , if we get an hanging thread then only following actions cab be taken.</p>
  
  <pre><code>1. unload the app-domain (this will stop appn)
  2. using flags and signaling to check exactly where is the thread and what is is doing and if all criteria are met then abort the thread. (i not sure about this)
  </code></pre>
  
  <p>Kindly let me know your opinion on this regard . Thanks lot. </p>