For loop goes out of range

后端 未结 3 598
滥情空心
滥情空心 2020-12-16 12:29
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        sta         


        
相关标签:
3条回答
  • 2020-12-16 13:08

    The reason is that you are using a loop variable inside a parallel task. Because tasks can execute concurrently the value of the loop variable may be different to the value it had when you started the task.

    You started the task inside the loop. By the time the task comes to querying the loop variable the loop has ended becuase the variable i is now beyond the stop point.

    That is:

    • i = 2 and the loop exits.
    • The task uses variable i (which is now 2)

    You should use Parallel.For to perform a loop body in parallel. Here is an example of how to use Parallel.For

    Alternativly, if you want to maintain you current strucuture, you can make a copy of i into a loop local variable and the loop local copy will maintain its value into the parallel task.

    e.g.

    for (int i = 0; i < 2; i++)
    {
      int localIndex = i;
      Task.Factory.StartNew(() => WorkerMethod(arr[localIndex])); 
    } 
    
    0 讨论(0)
  • 2020-12-16 13:10

    Using foreach does not throw:

    foreach (var i in arr)
    {
      Task.Factory.StartNew(() => WorkerMethod(i));
    }
    

    But is doesn't work either:

    101
    101
    

    It executes WorkerMethod with the last entry in the array. Why is nicely explained in the other answers.

    This does work:

    Parallel.ForEach(arr, 
                     item => Task.Factory.StartNew(() => WorkerMethod(item))
                     );
    

    Note

    This actually is my first hands-on experience with System.Threading.Tasks. I found this question, my naive answer and especially some of the other answers useful for my personal learning experience. I'll leave my answer up here because it might be useful for others.

    0 讨论(0)
  • 2020-12-16 13:25

    You are closing over loop variable. When it's time for WorkerMethod to get called, i can have the value of two, not the value of 0 or 1.

    When you use closures it's important to understand that you are not using the value that the variable has at the moment, you use the variable itself. So if you create lambdas in loop like so:

    for(int i = 0; i < 2; i++) {
        actions[i] = () => { Console.WriteLine(i) };
    }
    

    and later execute the actions, they all will print "2", because that's what the value of i is at the moment.

    Introducing a local variable inside the loop will solve your problem:

    for (int i = 0; i < 2; i++)
    {
        int index = i;
        Task.Factory.StartNew(() => WorkerMethod(arr[index])); 
    }
    

    <Resharper plug> That's one more reason to try Resharper - it gives a lot of warnings that help you catch the bugs like this one early. "Closing over a loop variable" is amongst them </Resharper plug>

    0 讨论(0)
提交回复
热议问题