0

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:

  1. Are threads collected by Finalizer thread as it do for other objects?
  2. Do I need to manually implement any cleaning up threads? Won't it taken care by CLR?
  3. I application is .NET 3.5. I know that I can go ahead with ThreadPool and TPL but due to various reasons I need to stick to .NET 3.5

Any suggestions/helps - much appreciated.

  • Possible duplicate of [Do we need to dispose or terminate a thread in C# after usage?](http://stackoverflow.com/questions/10698107/do-we-need-to-dispose-or-terminate-a-thread-in-c-sharp-after-usage) – Zein Makki Aug 11 '16 at 05:03
  • Also read this to answer you first question: http://stackoverflow.com/questions/3699147/c-sharp-thread-object-lifetime – Zein Makki Aug 11 '16 at 05:03
  • This is well suited to the ThreadPool. It is available in .Net 3.5, what is your reason for not using it? – FloatingKiwi Aug 11 '16 at 05:09
  • I agree with you @FloatingKiwi which I am planning in future. But as now I have to live with it :( – Sathishkumar Aug 11 '16 at 05:18
  • I'm not following this @Sathishkumar. You can use the ThreadPool even in .net2.0. TPL on the other hand is only available in .Net4 but you can still use the ThreadPool without it. – FloatingKiwi Aug 11 '16 at 05:21
  • thanks @user3185569, links are useful! – Sathishkumar Aug 11 '16 at 05:23
  • Agreed @FloatingKiwi, we can use TP in .NET 3.5. But I am bound to use this piece of code (as this is a very small fragment) in a huge application and requires complete overhaul. As of now, I am trying to find the cause for memory leak and unresponsive app. – Sathishkumar Aug 11 '16 at 05:26
  • You've launched 2000 threads, all competing for processor time, which may go to explaining the lack of responsiveness :) – FloatingKiwi Aug 11 '16 at 05:52
  • This was a specific problem in .NET 3.5, fixed in .NET 4. The Thread class does not have a Dispose() method and it needs one. It was omitted since there is no good way to call it. You'll get into trouble like this when the garbage collector doesn't run often enough. Workaround is to call GC.Collect() yourself, count the number of times you start a thread. "Various reasons" are never good enough reasons btw, getting stuck on a 8 year old version of free software is a pretty silly problem to have and one you *have* to fix some day. Like today. – Hans Passant Aug 11 '16 at 07:38

1 Answers1

0

.Net 3.5 still allows ThreadPool (it just doesn't allow TPL). To launch a thread simply do:

System.Threading.ThreadPool.QueueUserWorkItem(AddressOf DoSomethingElse)
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41