Shai
Shai

Reputation: 25595

Does a Thread stop itself once it's function scope is over?

Thread myThread = new Thread(new ParameterizedThreadStart(threadFunction));

public void threadFunction() {
 // Run a finite code
 ...
}

Question is: will myThread get disposed once threadFunction() is over?

Upvotes: 13

Views: 12506

Answers (2)

Parv Sharma
Parv Sharma

Reputation: 12705

YES.. the thread will stop and will be disposed once the function returns..
to hold the thread you will have to do something like

while(true){}

Alternatively if you dont want your thread to be disposed because creating a new thread consumes resources
you should use a ThreadPool there is a class with the same name in .Net.
so every time you need a thread it will be fetched from the thread pool and utilized

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 839114

Threads don't need to be disposed. The Thread class does not implement IDisposable and it does not have a Dispose method.

When your thread completes you don't need to do anything special to clean up.

Upvotes: 19

Related Questions