I have a few background threads that are running in a loop, that sometimes call native Win32 functions using PInvoke. I need to be able to stop such thread from the main thread as fast as I can. I used to do thread.Abort method, which stops the thread pretty fast, but if a thread was somewhere in Win32 native function at the moment it receives ThreadAbort exception it sometimes results in "dangling" handles/pointers and memory leaks. What is the fastest and safest way to stop such thread? Thanks!
Asked
Active
Viewed 42 times
0
-
Not a duplicate because I need to kill a thread as fast as I can, I can't wait for it to check a flag in a loop, because one loop itteration takes a lot of time. – user2102508 Nov 12 '18 at 10:13
-
You ask for the "fastest and safest". The duplicate does some suggestions, some for the fastest and some for the safest. Pick the one that suits you best. – Patrick Hofman Nov 12 '18 at 10:35
-
Fast is easy, but safe is not. There is no safe way to abort a thread. – Enigmativity Dec 02 '18 at 06:27