Threading Help....

Posted on 2004-11-15
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
    1 Comment
    LVL 14

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    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 …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now