4
我有大量需要执行的任务(〜1000)。我使用的是四核处理器,所以我想同时处理4个任务。.NET排队任务(带异步/等待)
为了给你一个出发点,下面是一些示例代码。
class Program
{
public class LongOperation
{
private static readonly Random RandomNumberGenerator = new Random(0);
const int UpdateFrequencyMilliseconds = 100;
public int CurrentProgress { get; set; }
public int TargetProcess { get; set; }
public LongOperation()
{
TargetProcess = RandomNumberGenerator.Next(
(int)TimeSpan.FromSeconds(5).TotalMilliseconds/UpdateFrequencyMilliseconds,
(int)TimeSpan.FromSeconds(10).TotalMilliseconds/UpdateFrequencyMilliseconds);
}
public async Task Execute()
{
while (!IsCompleted)
{
await Task.Delay(UpdateFrequencyMilliseconds);
CurrentProgress++;
}
}
public bool IsCompleted => CurrentProgress >= TargetProcess;
}
static void Main(string[] args)
{
Task.Factory.StartNew(async() =>
{
var operations = new List<LongOperation>();
for(var x = 1; x <= 10; x++)
operations.Add(new LongOperation());
await ProcessOperations(4, operations);
}).Wait();
}
public static async Task ProcessOperations(int maxSimultaneous, List<LongOperation> operations)
{
await Task.WhenAll(operations.Select(x => x.Execute()));
// TODO: Process up to 4 operations at a time, until every operation is completed.
}
}
我想我上会用什么班了,我怎么会结构ProcessOperations
处理最多同时4次操作,直到所有操作完成后,在一个单一的await-可以输入一些Task
。
我在考虑以某种方式使用SemaphoreSlim
对象,因为它似乎适用于限制资源/进程。
您应该查看[TPL DataFlow](https://msdn.microsoft.com/en-us/library/hh228603(v = vs.110).aspx)它是Microsoft为确切你正试图做的情况。你只需要用'ActionBlock'替换你的'List '并设置并行限制。 –
我知道我试图发明一个轮子,只是不知道在哪里找到它。谢谢! –