Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 509
Shai Avatar asked Jul 12 '12 09:07

Shai


2 Answers

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.

like image 85
Mark Byers Avatar answered Oct 16 '22 09:10

Mark Byers


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

like image 40
Parv Sharma Avatar answered Oct 16 '22 09:10

Parv Sharma