Threading Help....


I have the following sub that works just fine.  This code gets a list of 300+ folders from the P:\ Drive and a thread then will get a list of all the files in each folder and sort out the file with the latest date.    This works fine, except the server where P:\ is at is hung up for ~ 2 - 3 minutes processing each request from each thread.

I would like to only do say 20 - 40 threads a at time, but don't really know how to know when a thread is finished so I can add another to the pile.  

What I would like to do is use this function and have a loop inside that would only have 25 threads going at one time?  Or other suggestions would be welcomed.

Public Sub GetDirs()
        Dim dir As String
        Dim i As Integer = 1
        Dim j As Integer
        Dim dirs() As String = Directory.GetDirectories("P:\")
        ProgressBar1.Maximum = dirs.Length
        Dim tColl As Threading.ThreadPool
        For Each dir In dirs
            Dim objThread As Threading.Thread
            Dim objProcess As New MyThreadClass(New UpdateTreeviewHandler(AddressOf UpdateTreeview_Callback))
            objThread = New Threading.Thread(AddressOf objProcess.Process)
            objProcess.sFolder = dir
            objThread.Name = dir
            i += 1
            ProgressBar1.Value = ProgressBar1.Value + 1
        MsgBox("Finished getting Information")
    End Sub
Rog DManager Inforamtion SystemsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Of course you're more familiar with your code so perhaps this wouldn't work...

But may I suggest a global variable that increments everytime the objThread.Start() is called and decrements everytime the callback fires. Perhaps make another function called ThreadEnded which decrements the counter variable and then calls UpdateTreeview_Callback. Then you can just run a while loop that will delay the starting of any new threads until the thread count variable goes under the max number.

Hope this helps.
Spencer Ruport

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.