保持运行的任务的一个特定数 [英] Keep running a specific number of tasks

查看:167
本文介绍了保持运行的任务的一个特定数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力做到这一点:

I have been trying to do this:

创建N的任务来执行和保持运行TAKS此数目的一段时间,在此情况下 一个任务敲定,那么我应该开始一个新的任务,以保持任务数相同。

Create 'N' Task to execute and keep running this number of taks for a period of time, in that case the one task finalize, then i should start a new task to keep the same number of task.

我不知道这是可能的处理与的TaskScheduler或我要创建一个自定义的TaskScheduler。

I dont know if is this possible to handle with TaskScheduler or i have to create a custom TaskScheduler.

另一种选择,我认为可以工作的,使用TPL数据流生产者 - 消费者当任务完成,然后的TaskScheduler采取了新的任务由生产商产生。

Another option i think could work is , use TPL DataFlow Producer-Consumer when the task finish then taskscheduler take a new task generate by producer.

现在的问题是:如何创建一个新的任务时,一个成品保持任务相同数量的

The question is: how can i create a new task when one finished to keep the same number of tasks?

推荐答案

这code将继续运行 numTasks 任务并行。

This code will keep running numTasks Tasks in parallel.

int numTasks = 5;
SemaphoreSlim semaphore = new SemaphoreSlim(numTasks);
while(true)
{
    semaphore.Wait();
    Task.Run(() =>
        {
            DoSomething();
        })
        .ContinueWith(_ => semaphore.Release());
}

这篇关于保持运行的任务的一个特定数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆