Threading Help....

Posted on 2004-11-15
Medium Priority
Last Modified: 2010-04-23

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
Question by:Rog D
1 Comment
LVL 14

Accepted Solution

CyrexCore2k earned 375 total points
ID: 12590787
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Integration Management Part 2
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question