As Mark said, after your application has unhandled exceptions, all bets are largely disabled. To find out what using does, it takes code like this:
using(var myDisposableObject = GetDisposableObject()) {
and translates it into something like this:
MyDisposableObject myDisposableObject; try { myDisposableObject = GetDisposableObject(); // Do stuff with myDisposableObject } finally { if(myDisposableObject != null) { myDisposableObject.Dispose(); } }
So what happens when your application encounters an unhandled exception? An unhandled exception terminates the application. This termination (or any unexpected termination) may interfere with the execution of the finally block from your using statement.
You should always handle your exceptions, even if you do not surround your stream calls with try blocks. Consider connecting to the AppDomain.UnhandledException event to clear resources, log files, etc., before your application bites the dust.
EDIT
Just noticed that Hans posted something similar to AppDomain.UnhandledException, and he's right. It was in the case of any program that unexpected endings can give unexpected results. However, in your case, as suggested, do not rely on the complete execution of your application to complete, especially with file resources. Rather, consider writing your own process, to wait, even wait, to fail execution. Then your application can perform incomplete executions if necessary. You can create logs to track progress or mark steps in your process and evaluate them in each run to eliminate abnormal execution states.
Also, as another point, your classes (even considering the fact that they are just samples) are not thread safe ... you are not protecting your shared resources.
bitxwise Apr 25 '11 at 10:16 2011-04-25 10:16
source share