title: I need help creating a TaskScheduler to prevent threading overload
body: <p>I want to add workers into a queue, but only have the first N workers processing in parallel.  All samples I find are in C#.  </p>
  
  <p>This is probably simple for a programmer, but I'm not one.  I know enough about VB to write simple programs.  </p>
  
  <p>But my first application runs fine until it suddenly hits 100% CPU and then crashes.  Help, please (Yes, I've wasted 5 hours of work time searching before posting this...)</p>
  
  <p>More Context:  Performing a recursive inventory of directory structures, files, and permissions across file servers with over 1 million directories/subdirectories.</p>
  
  <p>Process runs serially, but will take months to complete.  Management already breathing on my neck.  When I try using Tasks, it goes to about 1000 threads, then hits 100% CPU, stops responding, then crashes.  This is on a 16 core server with 112 GB RAM.</p>
  
  <p>--Added
  So, with the sample provided on using Semaphores, this is what I've put in:</p>
  
  <pre><code>Public Class InvDir
      Private mSm as Semaphore
      Public Sub New(ByVal maxPrc As Integer)
          mSm = New Semaphore(maxPrc, maxPrc)
      End Sub
  
      Public Sub GetInventory(ByVal Path As String, ByRef Totals As Object, ByRef MyData As Object)
          mSm.WaitOne()
  
          Task.Factory.StartNew(Sub()
                  Dim CurDir As New IO.DirectoryInfo(Path)
                  Totals.SubDirectoryCount += CurDir.GetDirectories().Count
                  Totals.FilesCount += CurDir.GetFiles().Count
                  For Each CurFile As IO.FileInfo in CurDir.EnumerateFiles()
                      MyData.AddFile(CurFile.FileName, CurFile.Extension, CurFile.FullName, CurFile.Length)
                  Next
                  End Sub).ContinueWith(Function(x) mSm.Release())
      End Sub
  End Class
  </code></pre>