我在应用程序中创建普通线程asp.net
。线程完成后我该怎么办?离开它(它将返回线程池)或中止它。
Thread thread = new Thread(new ThreadStart(work));
我在应用程序中创建普通线程asp.net
。线程完成后我该怎么办?离开它(它将返回线程池)或中止它。
Thread thread = new Thread(new ThreadStart(work));
别管它。创建一个毫无意义的异常是没有意义的。
回想一下,该IDisposable
接口专门针对需要释放某些共享资源的场景而存在。(当然,它也被应用在其他情况下;但这就是它最初的用途。)
现在考虑托管Thread
类没有实现IDisposable
,您可能会(正确地)猜到它不需要任何特定的清理,超出了 GC 的正常处理。
在 C# 中使用线程池
[STAThread]
public static void Main(string[] args)
{
foreach(var fileNamePath in DirectoryFiles)
{
ThreadPool.QueueUserWorkItem(ThreadPoolCallback, fileNamePath);
}
}
public void ThreadPoolCallback(object threadContext)
{
//do something
}
.NET 中的 threadPool 处理所有其他事情。