title: Handle more than 64 thread at the same time
body: <p>I was reading a tutorial about <a href="http://msdn.microsoft.com/it-it/library/3dasc8as.aspx" rel="nofollow">Thread pooling in VB</a>. There was an example with Fibonacci calculations:</p>
  
  <pre><code>Imports System.Threading
  
  Module Module1
  
  Public Class Fibonacci
      Private _n As Integer
      Private _fibOfN
      Private _doneEvent As ManualResetEvent
  
      Public ReadOnly Property N() As Integer
          Get
              Return _n
          End Get
      End Property
  
      Public ReadOnly Property FibOfN() As Integer
          Get
              Return _fibOfN
          End Get
      End Property
  
      Sub New(ByVal n As Integer, ByVal doneEvent As ManualResetEvent)
          _n = n
          _doneEvent = doneEvent
      End Sub
  
      ' Wrapper method for use with the thread pool.
      Public Sub ThreadPoolCallBack(ByVal threadContext As Object)
          Dim threadIndex As Integer = CType(threadContext, Integer)
          Console.WriteLine("thread {0} started...", threadIndex)
          _fibOfN = Calculate(_n)
          Console.WriteLine("thread {0} result calculated...", threadIndex)
          _doneEvent.Set()
      End Sub
  
      Public Function Calculate(ByVal n As Integer) As Integer
          If n &lt;= 1 Then
              Return n
          End If
          Return Calculate(n - 1) + Calculate(n - 2)
      End Function
  
  End Class
  
  
  &lt;MTAThread()&gt; 
  Sub Main()
      Const FibonacciCalculations As Integer = 9 ' 0 to 9
  
      ' One event is used for each Fibonacci object
      Dim doneEvents(FibonacciCalculations) As ManualResetEvent
      Dim fibArray(FibonacciCalculations) As Fibonacci
      Dim r As New Random()
  
      ' Configure and start threads using ThreadPool.
      Console.WriteLine("launching {0} tasks...", FibonacciCalculations)
  
      For i As Integer = 0 To FibonacciCalculations
          doneEvents(i) = New ManualResetEvent(False)
          Dim f = New Fibonacci(r.Next(20, 40), doneEvents(i))
          fibArray(i) = f
          ThreadPool.QueueUserWorkItem(AddressOf f.ThreadPoolCallBack, i)
      Next
  
      ' Wait for all threads in pool to calculate.
      WaitHandle.WaitAll(doneEvents)
      Console.WriteLine("All calculations are complete.")
  
      ' Display the results.
      For i As Integer = 0 To FibonacciCalculations
          Dim f As Fibonacci = fibArray(i)
          Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN)
      Next
  End Sub
  
  End Module
  </code></pre>
  
  <p>I've start this module and it works correctly and this just handle 9 calculations:</p>
  
  <pre><code>Const FibonacciCalculations As Integer = 9
  </code></pre>
  
  <p>I've increase that limits, but this can just handle up to 63 calculations. From 64th calculation exception is raised that said:</p>
  
  <blockquote>
    <p>waithandle must be less than 64</p>
  </blockquote>
  
  <p>I would this application will handle N calculations. A good idea could be set a cap to pool of threads (for instance: 6). The N calculations will be handle using at most 6 thread at once. How could I edit the code to handle this removing the <code>waitHandle</code> error?</p>