C# Threads.Abort() C# Threads.Abort() multithreading multithreading

C# Threads.Abort()


Thread.Abort is not guaranteed to stop the thread and you should avoid using it if possible.

Calling this method usually terminates the thread.

Emphasis mine.

What it does is raise a ThreadAbortException in the target thread. If you catch this exception, the code will continue executing until it reaches the end of the catch block, at which point the exception is automatically rethrown. If you don't catch it, it is similar to a normal exception - it propagates up the call stack.

Assuming you don't catch the exception, all the code running in that thread will stop running. Other threads that were started from that thread will not be affected.


Anything started within that thread will be aborted.


You could be facing a race condition, where your main routine nulls the array before the ThreadAbortException reaches the func1 thread but after func2 checks for null array.

As a minimum, your main code and func2 should use a lock around the array. You should also test that the func1 thread is dead before restarting it again. And as everyone else has said, signal a thread to stop rather than relying on Thread.Abort.

I'm not 100% sure from your description that func2 is called from within the func1 thread, but if func2 is run on a different thread that's started from within func1, killing the func1 thread won't affect func2 as all threads exist as children of your parent process, not of the thread that they were started from.