Lu1zZz
Lu1zZz

Reputation: 57

Keep running a specific number of tasks

I have been trying to do this:

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.

I dont know if is this possible to handle with TaskScheduler or i have to create a custom 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?

Upvotes: 1

Views: 1371

Answers (2)

L.B
L.B

Reputation: 116118

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());
}

Upvotes: 8

xspydr
xspydr

Reputation: 3050

Task scheduler could be used to run an executable that you could use to perform a set of work/tasks. Alternatively, you could simply create a windows service...

Upvotes: 0

Related Questions