Should I ensure that threads end in Dispose?

1

Some questions about the interface IDisposable and using already exist, for example:

What types of features are released in a using statement

I should always use the dispose

I'm still not sure what to do about threads .

In my idea if the object uses some thread explicitly, it makes perfect sense to ensure this thread ends with the use of thread.join() for example. >

This would serve to ensure there is no work to be done in background but would also decrease the resources (in this case CPU) used by this threads .

In addition I also made sure that threads would be in a known state (terminated) instead of being in an unknown state.

I know that the CPU is not a resource that can be reserved by thread and that there is nothing to guarantee that my thread is using it. p>

But my premise is as follows: If I can assure you that there is no work to be done on threads , why not do it?

What should I do anyway? Should I make sure my threads end or not?

    
asked by anonymous 07.11.2016 / 15:18

2 answers

4

Bruno, whenever your class has members that implement IDisposable you MUST make the correct dispose of these members in your class.

Thread does not implement IDisposable , so you do not MUST , but you can END the thread for reasons you've commented.

But I suggest not using a Thread object directly, but instead using more modern features like ThreadPool or, better yet, Task (TPL).

    
07.11.2016 / 15:59
2

Minelli's answer already says the basics, I decided to answer because that was original in a question that I answered and had comments from me.

If the class Thread does not implement IDisposable you can not call Dispose() in any way, however much you want.

It may even be a terminology problem, but terminating a thread has nothing to do with discarding your operating system ( handle ) resources. The Dispose() is to discard, not to finish.

In question What types of features are released in a "using" statement? use of using , so there is no possibility of its use in class Thread , even if you want. You can do something to prevent the thread from staying alive if you want, but it will not be with using , it will not be calling Dispose() . No doubt you have to take some care of it to ensure its completion, but it is not with using . You have to make sure it's stopped, if you make sure, it's all right.

But creating threads is very expensive, it is not appropriate to create or kill them. You need to create and reuse what you have. For this we use tasks without it possible. Or we create a mechanism that simulates more or less what Task does.

The fact that a thread continues to exist does not mean that it is consuming any resources, it may be at standby without consuming any CPU, will only consume memory for your stack and some small admin information.

Already using the class Task , it is different, this class can, and usually should use using or a pattern that does more or less the same thing as using does.

    
07.11.2016 / 19:34