1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Proper use of ThreadPool.SetMaxThreads

Discussion in 'Visual Basic .NET' started by captchaman, Jun 1, 2011.

  1. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    No matter what I set it to whether it's ThreadPool.SetMaxThread(5, 5) or ThreadPool.SetMaxThreads(100, 100) it still only seems to spawn 8 threads at a time. Nowhere in my code is there anything to do with 8, but will post an example if needed.
     
  2. xhpdx

    xhpdx Regular Member

    Joined:
    Sep 21, 2008
    Messages:
    331
    Likes Received:
    2,160
    Occupation:
    Coder
    Location:
    EU
    I use it this way:
    Code:
    ThreadPool.SetMinThread(min,min)
    ThreadPool.SetMaxThread(min+1,min+1)
    ThreadPool.QueueUserWorkItem(New WaitCallback(DoSomething))
    If you're using .net 4 the TPL library is easier to implement with Parallel.ForEach and MaxDegreeOfParallelism
     
    • Thanks Thanks x 1
  3. shudogg

    shudogg Regular Member

    Joined:
    Sep 23, 2008
    Messages:
    412
    Likes Received:
    153
    Occupation:
    Internet Marketing
    Location:
    Indiana
    Home Page:
    Threadpool shouldn't be used to run any more than 25 threads per core processor. While now you can set the max threads for threadpool, you shouldn't.

    Even if YOU yourself have a dual core (or quad core) processor, if your making a software random clients will be using, some of them will still be on a single core processor. They should only run 25 threads. While your dual core people, could use 50 threads.. For a software made for your customers, you have to be dynamic.

    If you need more than 25 threads for your software, don't use threadpool.
     
    • Thanks Thanks x 1
  4. Hydrogen

    Hydrogen Newbie

    Joined:
    Dec 30, 2009
    Messages:
    39
    Likes Received:
    23
    Occupation:
    Co-Owner of AdvertMarketing
    Home Page:
    Couldn't have said it better myself, if you want to get real fancy with it as well. you can query windows API and find the exact number of cores/processor the user has and then dynamic set your options to make sure users don't overload their PC with your software. This achieves a few different things, 1.) makes your software appear more thought out and professional in the eyes of the users and 2.) and most importantly allows you to control the performance of your software.

    2.) is most important because if say you leave the thread selection up to the user and they crank it up to 100 and then start bitching and moaning that your software is slow and laggy that can be negative for potential future sales. If you control the max allowed threads of your app you keep the quality and performance high, earning praises from users. Some may ask why they have a 25 thread limit but if you say it's set dynamically based on the number of cores and processor they are using they will totally understand why and be more appreciative.
     
    • Thanks Thanks x 1
  5. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    This is great advice, I had no idea but it does explain bugs that users have reported in my software.

    @Hydrogen: Thanks for the tip, will be doing that from now on.



    Edit: According to http://stackoverflow.com/questions/1542213/how-to-find-the-number-of-cpu-cores-via-net-c you can calculate the number of cores in C#, but I'm having trouble converting that to VB.NET due to this line:
    Code:
    var In New System.Management.ManagementObjectSearcher
    (var is not defined, System.Management.ManagementObjectSearcher is not defined (even after importing System.Management)

    Now, I'm assuming
    Code:
    Environment.ProcessorCount / 2 * 25
    Won't be reliable because the author states that all the variables can be different
    So, what do I need to do to get the number of cores in VB.NET?

    Thanks
     
    Last edited: Sep 19, 2011
  6. Hydrogen

    Hydrogen Newbie

    Joined:
    Dec 30, 2009
    Messages:
    39
    Likes Received:
    23
    Occupation:
    Co-Owner of AdvertMarketing
    Home Page:
    ok first thing you need to do is go to Project / Reference and add System.Management

    This gives you access to more features of the PC.

    now you can create a small little function and run this. This is ready to go code, like I said if intellisense is yelling at you about System.Management.ManagementObjectSearcher then make sure you reference System.Management

    Code:
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim coreCount As Integer = 0
            For Each item In New System.Management.ManagementObjectSearcher("Select * from Win32_Processor").[Get]()
                coreCount += Integer.Parse(item("NumberOfCores").ToString())
            Next
            Dim CoreDisplay As String = "Number Of Cores: " & coreCount & vbCrLf & "Max Number of Threads you can use:" & (25 * coreCount)
            MsgBox(CoreDisplay)
        End Sub
    

    As with this question
    The answer would be 100 threads (4cores * 25 threads) = 100 Threads Max
     
    • Thanks Thanks x 1
    Last edited: Sep 19, 2011