none
TPL or Threadpool

    Question

  • Hi All,

    I am writing a very simple http server, which collects POST data (no more than 100 bytes) from some devices, then adds this to a database and finally the thread exits.

    Currently I am using the thread pool so I can handle several requests concurrently. My question is would I be best using the new TPL (Task.Factory.StartNew)? (Note: My threads do not return any data and or share variables, and are only alive for a few seconds’ possible mins if the internet is slow for some reason).

    I was going to run one thread and poll the request but this seem like a slow way of doing it.

    Is the TPL affected by the ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads? if not does it have an equivalent?

    Thanks for your help,

    Dora9

     


    Student [Visual C#.Net / VB.Net]
    Wednesday, January 19, 2011 10:09 PM

Answers

  • The TPL, internally, uses the ThreadPool.  You could just use Task.Factory.StartNew as a "replacement" for ThreadPool.QueueUserWorkItem.

     

    That being said, if you have the list of devices in a collection, you might want to consider using Parallel.For or Parallel.ForEach to process the entire collection in parallel.  This may actually do a better job since it will partition the work more intelligently than many individual tasks.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Dora9 Tuesday, January 25, 2011 4:11 PM
    Wednesday, January 19, 2011 10:18 PM
  • Setting ThreadPool.SetMinThreads will effect the TPL, with your code above, since it uses a ThreadPool thread.  In this case, you're blocking on the listener, so Task is probably a very good option.

     

    That being said, I would avoid setting a Max threads for the ThreadPool - just raise the min if required, and let it go.  Something like:

    int t;
    int c;
    
    ThreadPool.GetMinThreads(out t, out c);
    ThreadPool.SetMinThreads(System.Math.Max(t, 100), c);
    
    while(true)
    {
      // Block thread until new request
      HttpListenerContext request = myListner.GetContext();
      // Create new thread to process request then exit
      Task.Factory.StartNew(() => processData(request));
    }
    
    
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Dora9 Tuesday, January 25, 2011 4:11 PM
    Wednesday, January 19, 2011 10:39 PM

All replies

  • The TPL, internally, uses the ThreadPool.  You could just use Task.Factory.StartNew as a "replacement" for ThreadPool.QueueUserWorkItem.

     

    That being said, if you have the list of devices in a collection, you might want to consider using Parallel.For or Parallel.ForEach to process the entire collection in parallel.  This may actually do a better job since it will partition the work more intelligently than many individual tasks.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Dora9 Tuesday, January 25, 2011 4:11 PM
    Wednesday, January 19, 2011 10:18 PM
  • Hi Reed,

    Thanks for your response.

    Will changing the ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads values affect the TPL as well? As in my application I set the MinThreads to 50 in an effort to stop the thread pool from delaying the creation of threads (I am running this on a quad core server).

    I would have liked to use the Parallel.For and Parallel.ForEach but I cannot see how I could use it for my code: -

    while(true)<br/>
    {<br/>
    // Block thread until new request
     HttpListenerContext request = myListner.GetContext();
    // Create new thread to process request then exit
    Task.Factory.StartNew(() => processData(request));<br/>
    }
    

    or

    int t = 0;
    int c = 0;
    
    ThreadPool.GetMinThreads(out t, out c);
    ThreadPool.SetMinThreads(50, c).ToString();
    
    ThreadPool.GetMaxThreads(out t, out c);
    ThreadPool.SetMaxThreads(100, c).ToString();
    
    while(true)
    {
    // Block thread until new request
    HttpListenerContext request = myListner.GetContext();
    // Create new thread to process request then exit
    ThreadPool.QueueUserWorkItem(processData, (Object)request);
    }
    

    Thanks

    Dora9

     


    Student [Visual C#.Net / VB.Net]
    Wednesday, January 19, 2011 10:31 PM
  • Setting ThreadPool.SetMinThreads will effect the TPL, with your code above, since it uses a ThreadPool thread.  In this case, you're blocking on the listener, so Task is probably a very good option.

     

    That being said, I would avoid setting a Max threads for the ThreadPool - just raise the min if required, and let it go.  Something like:

    int t;
    int c;
    
    ThreadPool.GetMinThreads(out t, out c);
    ThreadPool.SetMinThreads(System.Math.Max(t, 100), c);
    
    while(true)
    {
      // Block thread until new request
      HttpListenerContext request = myListner.GetContext();
      // Create new thread to process request then exit
      Task.Factory.StartNew(() => processData(request));
    }
    
    
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Dora9 Tuesday, January 25, 2011 4:11 PM
    Wednesday, January 19, 2011 10:39 PM
  • Hi Reed,

    Thanks for your advice, is it worth looking into the Async model in .Net 4.0 for my application rather than create loads of threads that exit and then eventually are destroyed.

    If so are there any good examples that you know of? Or am I best sticking with the thread pool?

    NOTE: The processData thread / method, simple does a HTTPOK response to the calling / posting device than parses some strings and adds the data to a database using LINQ to entities (so not sure if i can use the Async model).

    Thanks,

    Dora9

     

     

     

     

     


    Student [Visual C#.Net / VB.Net]
    Wednesday, January 19, 2011 11:05 PM
  • Hi,

    Any update? Has your question been resolved?

    Best Regards,

    Larcolais


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 25, 2011 3:42 PM
  • Hi,

    Yes it looks like i will have to use the Async modle instead of threads. When is C# 5.0 and async, await model being released?

    Thanks,

    Dora9


    Student [Visual C#.Net / VB.Net]
    Tuesday, January 25, 2011 4:12 PM
  • Yes it looks like i will have to use the Async modle instead of threads. When is C# 5.0 and async, await model being released?

     

    This will be released with the next version of .NET.  There has been no official word on a release date, but I suspect it's not coming soon. 

     

    That being said, you can do asynchronous programming today, in .NET 4.  (Its been possible to do 100% asynchronous networking since .NET 1.1...)  The C# 5 async/await feature just makes it easier to use..

     

    Look at Task.Factory.FromAsync(...) for examples of how you can build asynchronous code using the TPL with the begin/end method pairs.  I blogged about how to wrap today's code into Task/Task<T>, which will work with async/await, but also works with Task Continuations (today).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, January 25, 2011 4:21 PM