I have below piece of code in my application. The applicaiton is an WindowsService and DoSomethingInTheading method is fired once the service is started.
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
Now everything is seems to be fine when I have around 2500~3000 threads. Once it starts to build up after this limit I get application running out of memory or sometimes the application stops to respond. My assumption was that after the task is finished thread will be cleared up by GC (meaning in Finalizer thread).
My questions are:
Any suggestions/helps - much appreciated.
.Net 3.5 still allows ThreadPool (it just doesn't allow TPL). To launch a thread simply do:
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf DoSomethingElse)