I have an application, which has run without issue for a long time, which suddenly fails to start due to the following error:
"This property has already been set and cannot be modified."
When I inspect the code, which basically resembles the snippet below, I see the exception is thrown on the line which attempts to name the first task inside Parallel.Invoke
Thread.CurrentThread.Name = "Main Program Thread";
// Do some start up tasks in parallel
Parallel.Invoke(new ParallelOptions { MaxDegreeOfParallelism = 10 },
() =>
{
Thread.CurrentThread.Name = "First thread";
},
() =>
{
Thread.CurrentThread.Name = "Second thread";
});
...
Obviously the cause of this must be that the main thread already has a name, and the first task is being run on the main thread rather than a threadpool thread.
Whilst I can resolve this by not naming the threads inside Parallel.Invoke, I am curious as to why this has suddenly started happening. Is it the case that normally Parallel.Invoke() previously ran all its tasks on threadpool threads and for some reason is unable to do so anymore? What could trigger this sort of thing?
The more I look at this code the more perplexed I am that it has ever worked. It looks to me like this code should always throw an exception.