对集合中的每个项目运行一个操作,逐个WITH A PAUSE

时间:2018-03-28 19:17:46

标签: c# linq task

我需要为集合中的每个项目执行任务,逐个(不是并行)暂停以防止DB服务器发热。这是我写的一种方法:

public static Task ForEachWithPause<T>(this IEnumerable<T> enumeration, Action<T> action, int milliseconds)
{
    return Task.Run(async () =>
    {
        foreach (T item in enumeration)
        {
            action(item);
            await Task.Delay(milliseconds);
        }
    });
}

//usage
intCollection..ForEachWithPause(integer =>
    {
        //do something with integer
    }, 500);

有人发现它有什么问题吗?我对任务和async

都很陌生

0 个答案:

没有答案