Answer the question
In order to leave comments, you need to log in
How to parallelize the execution of the "For" loop?
Hello.
I used a loop for
to write a two-dimensional string array in Excel:
for (var column = 1; column <= columns; column++)
{
// Тело цикла, выполняющее функцию записи одного столбца в Excel документ.
}
for
:Parallel.For(1, columns + 1, column =>
{
// Тело цикла, выполняющее функцию записи одного столбца в Excel документ.
});
for
, this problem does not arise.
Answer the question
In order to leave comments, you need to log in
Race Condition apparently. Welcome to the world of multithreading. With parallel execution, there is no guarantee that the order will be preserved. Whoever got up first, that and slippers.
I would advise you to break the array (or whatever you have in the loop) into 2, 3 ... N parts, strictly assigning each part its own areas (say, the first thread fills columns 1 ... 10, the second - 11..20 etc.) and run N synchronous loops on different threads.
And in principle, in order to understand how multithreading works, I would first try to do everything manually, using the good old System.Threading.Thread. In Tasks, a lot is hidden "under the hood", and you can step on a lot of rakes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question