Naming the stream inside Parallel.Invoke raises the exception "This property is already set and cannot be changed."

I have an application that runs without problems for a long time, which unexpectedly does not start due to the following error:

"This property is already set and cannot be changed."

When I check the code, which basically looks like the snippet below, I see that the exception is thrown on the line that is trying 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 reason for this should be that the main thread already has a name, and the first task is performed in the main thread, and not in the thread thread.

, Parallel.Invoke, , . , Parallel.Invoke() threadpool - ? ?

, , - . , .

+4
3

Parallel , - , . , .

, , . , . .

. , .

LongRunning , .

+1

. , threadpool, . , , , , .

, Parallel.Invoke , , , - , , .

0

I uninstalled .NET 4.6 and this code started working again. In Parallel.Invoke, there should be optimization in newer versions of the structure that displayed an error in our code, where it had never appeared before.

0
source

Source: https://habr.com/ru/post/1598200/


All Articles