none
How to provide Minimum & Maximum number of Tasks that needs to be created, similar to ThreaPool min & max Threads. RRS feed

  • Question

  • In the previous versions of .NET we can provide Minimum & Maximum Threads that need to be available to our flow using "ThreadPool.SetMinThreads()". Similarly while using Tasks how can we distribute the Tasks that needs to be created among various flows in our application, so that heavily loaded flows get more Tasks instead of waiting for availability in the pool.
    Thursday, June 2, 2011 6:28 AM

Answers

  • You can do this by creating a custom TaskScheduler that is used to schedule your tasks.

     

    For an example, see the How To: Create a Task Scheduler that Limits Concurrency.

     

    Once you've created this, you can use it to schedule tasks that are restricted by doing:

    var scheduler = CreateCustomTaskScheduler();
    var factory = new TaskFactory(scheduler); // This factory will schedule using your custom scheduler
    
    var task = factory.StartNew( () => DoSomething() );
    

    You can also specify the scheduler for use with the Parallel class by using the overloads that take a ParallelOptions.

     


    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 eryang Tuesday, June 14, 2011 3:08 AM
    Thursday, June 2, 2011 4:24 PM
    Moderator
  • We temporarily mark a reply since the thread idle for a long time, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Eric Yang [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, June 14, 2011 3:08 AM

All replies

  • You can do this by creating a custom TaskScheduler that is used to schedule your tasks.

     

    For an example, see the How To: Create a Task Scheduler that Limits Concurrency.

     

    Once you've created this, you can use it to schedule tasks that are restricted by doing:

    var scheduler = CreateCustomTaskScheduler();
    var factory = new TaskFactory(scheduler); // This factory will schedule using your custom scheduler
    
    var task = factory.StartNew( () => DoSomething() );
    

    You can also specify the scheduler for use with the Parallel class by using the overloads that take a ParallelOptions.

     


    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 eryang Tuesday, June 14, 2011 3:08 AM
    Thursday, June 2, 2011 4:24 PM
    Moderator
  • We temporarily mark a reply since the thread idle for a long time, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Eric Yang [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, June 14, 2011 3:08 AM