How do I properly cancel Parallel.Foreach?
问题 I've got code which looks something like this: Parallel.Foreach(ItemSource(),(item)=>DoSomething(item)); ItemSource() produces an infinite stream of items. I want the loop to exit once some condition has been met, and I'd rather do it without throwing an exception in DoSomething (I consider this approach a bad programming style). Ideally, there would be something like cancellationToken. I would call cancellationToken.Activate() in one or more threads, after which parallel.foreach would stop