foreach (var action in actions)
{
Task.Factory.StartNew(action);
}
But, what if I need to limit the number of actions that run in the same time? How do I do that?
I really like microsoft's solution here http://msdn.microsoft.com/en-us/library/ee789351.aspx .
Example usage for only allowing one thread at a time:
LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(1);
TaskFactory factory = new TaskFactory(lcts);
factory.StartNew(()=>
{
//work as usual
});
consider using a BlockingCollection and GetConsumingEnumerable() inside your action for a pretty clean implementation http://msdn.microsoft.com/en-us/library/dd287186.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.