Вы не должны использовать параллельные библиотеки из вашего потока пользовательского интерфейса.Параллельная библиотека запускает группу задач в нескольких потоках, поэтому вам не следует писать внутри нее какой-либо код, связанный с пользовательским интерфейсом.
Что вам нужно сделать, это переместить бизнес-логику в фоновые задачи и обновить пользовательский интерфейс с помощью диспетчера, который будетвыполнить его в потоке пользовательского интерфейса
, так как MSDN говорит
It is important to keep your application's user interface (UI) responsive. If an
operation contains enough work to warrant parallelization, then it likely should not
be run that operation on the UI thread. Instead, it should offload that operation to
be run on a background thread. For example, if you want to use a parallel loop to
compute some data that should then be rendered into a UI control, you should consider
executing the loop within a task instance rather than directly in a UI event handler.
Only when the core computation has completed should you then marshal the UI update back
to the UI thread.
и, что особенно важно, если вы пытаетесь обновить поток пользовательского интерфейса из Paralle.Foreach
If you do run parallel loops on the UI thread, be careful to avoid updating UI
controls from within the loop. Attempting to update UI controls from within a parallel
loop that is executing on the UI thread can lead to state corruption, exceptions,
delayed updates, and even deadlocks, depending on how the UI update is invoked