Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Abort in .NET

I have a thread 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.

like image 447
Will Avatar asked Dec 03 '22 06:12

Will


1 Answers

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.

like image 102
Marc Gravell Avatar answered Jan 31 '23 07:01

Marc Gravell