Should I explicitly set thread reference to null after thread completes it’s work or it’s not necessary? If I want to set it as null then where in the code should I do this?
Should I check if thread is alive or not using thread.isalive flag and then set it to null if it’s not alive?
Personally, I would not bother. There are a few cases where explicitly setting object references to null really matters anyway. Regardless of type those cases are:
Those are the ones I could think of. Perhaps there are more, but probably not many. The common denominator here is that the variable references an object that consumes a lot of memory. The managed
Threadobject itself is not going to consume a lot of memory so I cannot imagine that setting a variable of typeThreadto null is going to accomplish a whole lot for you regardless of how it is used. Most of the resources are allocated when the thread starts and then get deallocated when the thread ends.I prefer to use
Thread.Jointo test and wait for the completion of a thread. In fact, I have never usedThread.IsAlivefor anything useful…ever.1The GC is actually smart enough to dispose local object references before a method ends if it can detect that the reference is no longer used.