I'm using Parallel.ForEach for achieving parallelism, I was wondering is there any way that I can stop the execution of Parallel.ForEach once any one the requests complete. For example consider the following code:
IEnumerable<string> myList = new List<string>() { "admin", "admin2", "admin3" }; CancellationTokenSource _tokenSource = new CancellationTokenSource(); Parallel.ForEach(myList, admin_name => { Console.WriteLine(admin_name); }); The above code prints admin,admin2 and admin3. I want to stop further execution if any of the admin names is printed.
Is this achievable? Or is there any work around available?
I am open to suggestions and improvement.
ParallelLoopState(anotherForEachoverload has a 2nd parameter foraction).