I'm experiencing really strange behavior, which I cannot neither understand nor explain. I was able to create a really simple sample to demonstrate that. That was reproduced on VS.Net 2013 & 2015 on different machines with several target .Net versions.
using System;
using System.Linq;
namespace Sample
{
internal static class Program
{
private static void Main(string[] args)
{
ExecuteInParallel(new[]
{
new Tuple<string, Action>("Task1", () => { }),
new Tuple<string, Action>("Task2", () => { throw new Exception("Exception"); })
});
}
private static void ExecuteInParallel(Tuple<string, Action>[] tasks)
{
tasks.AsParallel().ForAll(x =>
{
Console.WriteLine("Starting " + x.Item1);
x.Item2();
});
}
}
}
When you just run that code without debugging, you receive what expected: two task started, one failed, and AggregateException details in console:
Starting Task1
Starting Task2
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.Exception: Exception
at Sample.Program.<Main>b__1() in d:\temp\ConsoleApplication40\ConsoleApplication40\Program.cs:line 13
...
But when you start the same code with debugging, results are really weird. First, you receive AggregateException:
But when you click Continue, it just starts both tasks again and again - here is sample output from Console:
Starting Task1
Starting Task2
Starting Task1
Starting Task2
Starting Task1
Starting Task2
Starting Task1
Starting Task2
I was using two tasks to demonstrate that they are both restarting; you can comment out empty one and receive the same confusing results.
My question here - is this a VS.Net bug, or some feature I don't understand?
Is there a way to debug in a "normal" way here?