In many cases (but definitely not all), you can protect against ThreadAbortException . Most of the critical code in .NET BCL does this pretty well already. The problem is that it is really complicated. And for this reason, most people recommend, and rightly so, to avoid interrupting threads. Starting with version 2.0, the CLR made the flow more intermittent and introduced a new set of APIs to help code developers protect them. Take a look at Limited Runtime Areas for an in-depth look at how this all works.
I believe that you relate correctly to your problems with an example using block. For regions with limited execution to work correctly, out-of-band (asynchronous) exceptions must occur from within the try block. But due to the using extension, the expression is evaluated outside the try block. Contrast this with the extension of the lock block, which evaluates the expression from the try block. Well, this is true with version 4.0 of the framework anyway, and it has been specifically modified to protect against these exceptions.
So, the question is why the same change was not made with the using block. According to Joe Duffy , this was an acceptable omission, since the assumption is that the interruption of the stream should always be accompanied by the termination of the application, which should in any case.
So yes. Your code does not tolerate out-of-band (asynchronous) exceptions. But the prevailing wisdom from the smart than me is that it should not be.
source share