.NET 4.5和c#的基于任务的处理,并发任务号有限制 [英] Task based processing with a limit for concurrent task number with .NET 4.5 and c#
问题描述
在此之后,任何人都可以发布针对以下任务的准系统解决方案,目标是.NET 4.5(以及更大范围的WPF UI)?
Following this, could anyone post a barebone solution for the following task, targeting .NET 4.5 (and WPF UI on a bigger scale)?
基本上,我正在寻找与在.NET 4.5和c#上实现的任何torrent应用程序类似的功能.
Basically I'm looking for a functional analog of any torrent application implemented on .NET 4.5 and c#.
任务:
我有IEnumerable<IProcessable>
,包含1000个IProcessable
实例,IProcessable
具有Process(int argument)
方法,执行需要1到10秒.我想遍历集合并处理IProcessable
的每个实例,将并发处理的实例数限制为N(1..10),最大并发实例数应易于调整.理想情况下,我每个IProcessable
都要报告有关流程完成进度的信息,这是Process的原型(为了启用进度报告,可能需要将其转换为某种东西而不是无效的东西):
I have IEnumerable<IProcessable>
, containing 1000 instances of IProcessable
, IProcessable
has Process(int argument)
method, taking from 1 to 10 seconds to execute. I want to loop through the collection and process each instance of IProcessable
, limiting the number of concurrently processed instances to N (1..10), number of max concurrent instances should be easily adjustable. Ideally I'd each IProcessable
to report about the progress of Process completion, here's a prototype of Process (it probably needs to be converted to something rather than void in order to enable progress reporting):
void Process(int e)
{
int progress = 0;
...Sleep for 100ms;
int progress = 30;
...Sleep for 100ms;
int progress = 50;
...
}
推荐答案
如果只想一次限制处理的实例数,那么 MaxDegreeOfParallelism
,这样做.
If all you want is to limit the number processed instances at a time, it seems like Parallel.ForEach()
would be a good solution. When you use it, you can specify MaxDegreeOfParallelism
, which does that.
这篇关于.NET 4.5和c#的基于任务的处理,并发任务号有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!