What should I do with a thread when it's done ? leave it or abort it?
-
10-10-2019 - |
Question
I create normal threads in asp.net
application. After the thread is done what should I do ? leave it (it will get back to thread pool) or abort it.
Thread thread = new Thread(new ThreadStart(work));
Solution
Leave it. There is no sense in creating a pointless exception.
OTHER TIPS
Recall that the IDisposable
interface exists specifically for the scenario where some shared resource needs to be released. (It has been applied in other contexts as well, of course; but that is the situation it was originally meant for.)
Now consider that the managed Thread
class does not implement IDisposable
and you might guess (correctly) that it does not require any specific cleanup beyond normal handling by the GC.
using threadpools in C#
[STAThread]
public static void Main(string[] args)
{
foreach(var fileNamePath in DirectoryFiles)
{
ThreadPool.QueueUserWorkItem(ThreadPoolCallback, fileNamePath);
}
}
public void ThreadPoolCallback(object threadContext)
{
//do something
}
The threadPool in .NET handles everything else.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow