并行ForEach在生成之前等待500毫秒

编程入门 行业动态 更新时间:2024-10-08 10:56:34
本文介绍了并行ForEach在生成之前等待500毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这种情况:

var tasks = new List<ITask> ... Parallel.ForEach(tasks, currentTask => currentTask.Execute() );

是否可以指示PLinq等待500ms,直到产生下一个线程?

Is it possible to instruct PLinq to wait for 500ms before the next thread is spawned?

System.Threading.Thread.Sleep(5000);

推荐答案

您正在使用 Parallel.Foreach 完全错误,您应该制作一个特殊的枚举器,将其速率限制为每500毫秒获取一次数据

You are using Parallel.Foreach totally wrong, You should make a special Enumerator that rate limits itself to getting data once every 500 ms.

由于您未提供任何详细信息,因此我对您的 DTO 的工作方式进行了一些假设.

I made some assumptions on how your DTO works due to you not providing any details.

private IEnumerator<SomeResource> GetRateLimitedResource() { SomeResource someResource = null; do { someResource = _remoteProvider.GetData(); if(someResource != null) { yield return someResource; Thread.Sleep(500); } } while (someResource != null); }

这就是您的并购外观

Parallel.ForEach(GetRateLimitedResource(), SomeFunctionToProcessSomeResource);

更多推荐

并行ForEach在生成之前等待500毫秒

本文发布于:2023-05-31 14:00:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/393025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ForEach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!