.NET Thread Finalizer线程问题

时间:2016-08-11 04:56:40

标签: vb.net multithreading garbage-collection .net-3.5 clr

我的应用程序中有一段代码。应用程序是WindowsService,并且在服务启动后会触发DoSomethingInTheading方法。

Public Sub DoSomethingInThreading()
    While True
    Dim completeEvent As New ManualResetEvent(False)
    Dim thread as Thread = New Thread(AddressOf DoSomeThingElse)
    If Not completeEvent.WaitOne(120000) Then
        Throw New SystemException("Issue in terminating")
    End If
    End While
End Sub

Public Sub DoSomeThingElse(Dim state as Object)
    Dim finishEvent as ManualResetEvent = DirectCast(state, ManualResetEvent)
    'Do I/O operation, this doesnt take more time (hardly in seconds)
    finishEvent.Set()
End Sub

现在,当我有大约2500~3000个线程时,一切似乎都很好。一旦它在此限制之后开始累积,我就会使应用程序内存不足或有时应用程序停止响应。我的假设是在任务完成后,线程将被GC清除(意味着在Finalizer线程中)。

我的问题是:

  1. Finalizer线程收集的线程是否与其他对象一样?
  2. 我是否需要手动执行任何清理线程? CLR不会照顾它吗?
  3. 我的应用程序是.NET 3.5。我知道我可以继续使用ThreadPool和TPL,但由于各种原因我需要坚持使用.NET 3.5
  4. 任何建议/帮助 - 非常感谢。

1 个答案:

答案 0 :(得分:0)

.Net 3.5仍然允许ThreadPool(它不允许TPL)。要启动一个线程,只需执行以下操作:

System.Threading.ThreadPool.QueueUserWorkItem(AddressOf DoSomethingElse)