具有返回值的并行任务队列

时间:2018-01-26 04:39:46

标签: c# .net queue task

我有一个实现问题,主要是排队返回值的长时间运行的任务。由于此任务中的CPU使用率,我希望将同时运行的任务数限制为常数。

我正在将一个异步任务返回给它将要等待的调用者,并希望实现仍然会发生这种情况。

这是我要排队的任务,让调用者等待:

public async Task<string> LongTask(string filename)
{
    return await Task.Run(() =>
    {
        //does something for awhile
        return "my result";
    });
}

这是从调用者调用它的方式,它将等待结果:

string result = await LongTask("test");

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下功能:

int nMaxConcurrentTasks = 5;
public void DoSomethingALotWithTasksThrottled()
{
    var listOfTasks = new List<Task>();
    for (int i = 0; i < 100; i++)
    {
        var count = i;
        // Note that we create the Task here, but do not start it.
        listOfTasks.Add(new Task(() => Something(count)));
    }
    Tasks.StartAndWaitAllThrottled(listOfTasks, nMaxConcurrentTasks); // 5 max
}

public static void StartAndWaitAllThrottled(IEnumerable<Task> tasksToRun, int maxActionsToRunInParallel, CancellationToken cancellationToken = new CancellationToken())
{
    StartAndWaitAllThrottled(tasksToRun, maxActionsToRunInParallel, -1, cancellationToken);
}


public static void StartAndWaitAllThrottled(IEnumerable<Task> tasksToRun, int maxActionsToRunInParallel, int timeoutInMilliseconds, CancellationToken cancellationToken = new CancellationToken())
{
    // Convert to a list of tasks so that we don't enumerate over it multiple times needlessly.
    var tasks = tasksToRun.ToList();

    using (var throttler = new SemaphoreSlim(maxActionsToRunInParallel))
    {
        var postTaskTasks = new List<Task>();

        // Have each task notify the throttler when it completes so that it decrements the number of tasks currently running.
        tasks.ForEach(t => postTaskTasks.Add(t.ContinueWith(tsk => throttler.Release())));

        // Start running each task.
        foreach (var task in tasks)
        {
            // Increment the number of tasks currently running and wait if too many are running.
            throttler.Wait(timeoutInMilliseconds, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            task.Start();
        }

        // Wait for all of the provided tasks to complete.
        // We wait on the list of "post" tasks instead of the original tasks, otherwise there is a potential race condition where the throttler&#39;s using block is exited before some Tasks have had their "post" action completed, which references the throttler, resulting in an exception due to accessing a disposed object.
        Task.WaitAll(postTaskTasks.ToArray(), cancellationToken);
    }
}

原创文章here