I have a thre开发者_高级运维ad thats analyzing a file and making transactional calls to the database, each transaction has a audit entry as part of its transaction. Is there anything vastly wrong with calling Thread.Abort() to stop the processing of the file? Rather than scatter ugly safe spots everywhere?
The file will be closed after the Abort call.
The obvious problem would be the risk of abandoning a transaction, which could cause blocking for an indeterminate time (or until timeout). But you could leave all sorts off mess - unrecoverable Monitor
locks, semaphores, etc, memory leaks, etc.
In short: try as hard as possible to avoid having to abort a thread unless you know exactly what it is doing at the time. And the only way you could know what it is doing is if it is already in a known "safe spot" (to use your term) - so you might as well just throw an exception or something and let it raise itself in a managed way.
Best practice: only abort a thread when you are tearing down the process.
Try Thread.Interrupt() to force the thread to exit from its wait state.
MSDN page
精彩评论