multithreading - What is the best way to throttle many .NET Task instances? -
i have created large amount of task instances. need run them all, , wait them complete. problem need make sure no more x tasks between "started" , "completed" @ given time; tasks involve calls other parties have restriction on number of simultaneous calls. since these restrictions not based on hardware, can't rely on built in intelligent throttling; need enforce limit strictly. i've been able having tasks increment , decrement shared variable in thread-safe way, seems unnecessarily cumbersome. there way that's more built-in api directly, or easy synchronization tool i'm missing?
you have create own task scheduler, see how to: create task scheduler limits degree of concurrency
edit: requires more code semaphore, have feeling might perform better, because thread pool in tasks executed unaware of semaphore, using taskscheduler playing rules.
edit 2: 1 of possible drawback of using semaphore thread pool might think task doing io operations , schedule them lot @ time (so hang there , wait, because don't need cpu). using taskscheduler scheduled when there place them. keep pool cleaner.
Comments
Post a Comment