简体   繁体   English

在Parallel.ForEach循环内使用方法

[英]Use methods inside Parallel.ForEach loop

I should use Parallel.ForEach loop, and inside there should be call to method. 我应该使用Parallel.ForEach循环,并且应该在其中调用方法。

Parallel.ForEach(myList, item => {  
   DoSomethingWithItem(item);
 }
);

Should that method be Task or whatever? 该方法应该是Task还是其他?

private Task DoSomethingWithItem(MyClass item);

Also, this method shouldn't have return type, but I have warning "not all code paths return a value". 另外,此方法不应具有返回类型,但我会警告“并非所有代码路径都返回值”。 Tried to put void keyword but seems it doesn't go in same context with Task keyword. 试图放置void关键字,但似乎与Task关键字不在同一上下文中。

No 没有

Do not make it more complicated than it has to be. 不要使其变得比必须的复杂。

Parallel.ForEach(myList, item => {  
   DoSomethingWithItem(item);
 }
);

is perfectly fine for void DoSomethingWithItem(MyClass item) . 对于void DoSomethingWithItem(MyClass item)是完美的。

Making it "more" async is overkill at best, counterproductive in the worst case. 使其“更多”异步充其量是过大的,在最坏的情况下适得其反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM