Keep running a specific number of tasks

前端 未结 2 869
太阳男子
太阳男子 2020-12-11 12:20

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 sho

相关标签:
2条回答
  • 2020-12-11 13:01

    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());
    }
    
    0 讨论(0)
  • 2020-12-11 13:09

    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...

    0 讨论(0)
提交回复
热议问题