This is what my loop looks like:
var loopResult = Parallel.ForEach(folder.Items.Cast<object>(), (item, loopState) =>
{
if (!loadData)
{
loopState.Stop();
return;
}
DoSomeWork(item);
}
);
if (loopResult.IsCompleted)
{
Debug.WriteLine("done");
}
The problem is the code never gets to if (loopResult.IsCompleted). After executing return; for all the different threads, absolutely nothing happens.
The code never gets to the body of
if (loopResult.IsCompleted)because the parallel loop has been stopped successfully. You can check it as follows:You can find useful information how to
Stop/Breaka parallel loop in this MSDN page, starting from Breaking Out of Loops Early subsection.