I created a ThreadQueue to allow for more efficient loading of WPF pages without freezing the main program due to many, many calculations done during the loading period. The threads typically access NHibernate (which I know little about because the company I work for created a wrapper dll for it.)
I have code set up to Abort the threads if the user determines they no longer need to view the page. I know that is wrong the wrong way to do this, but currently seems to be the most efficient practice. In other threaded programs I have written I will use global variables and set those in loops to allow the thread to quickly and gracefully die. This program uses many static classes to do the majority of the calculations (big, long calculations; many can take up to 5 min to run). That being said, putting global die booleans in a static class could potentially kill off many threads running through those calculations.
Now to the issue at hand: Abort is bad, so are global die variables in a static class. One of the key issues of threading is Deadlock. Will killing a thread in the middle of a transaction cause any deadlock problems? Any suggestions on how to kill a thread gracefully during a lengthy calculation in a static class?
It would be better to use
TaskswithCancellationTokens. This link may help