当多线程时,循环索引超出范围ArgumentOutOfRangeException

时间:2010-04-30 01:53:16

标签: c# multithreading exception lambda for-loop

我遇到了一些奇怪的行为......当我在dummyText方法中迭代List ThreadTest时,我得到一个超出范围异常的索引(ArgumentOutOfRangeException ),但如果我删除线程,我只打印出文本,那么一切正常。

这是我的主要方法:

public static Object sync = new Object();
static void Main(string[] args)
{
    ThreadTest();
    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

此方法抛出异常:

private static void ThreadTest()
{
    Console.WriteLine("Running ThreadTest");
    Console.WriteLine("Running ThreadTest");
    List<String> dummyText = new List<string>()
    { "One", "Two", "Three", "Four", "Five", 
      "Six", "Seven", "Eight", "Nine", "Ten"};

    for (int i = 0; i < dummyText.Count; i++)
    {
        Thread t = new Thread(() => PrintThreadName(dummyText[i])); // <-- Index out of range?!?
        t.Name = ("Thread " + (i));
        t.IsBackground = true;
        t.Start();
    }
}

private static void PrintThreadName(String text)
{
    Random rand = new Random(DateTime.Now.Millisecond);
    while (true)
    {
        lock (sync)
        {
            Console.WriteLine(Thread.CurrentThread.Name + " running " + text);
            Thread.Sleep(1000+rand.Next(0,2000));
        }
    }
}

这不会抛出异常:

private static void ThreadTest()
{
    Console.WriteLine("Running ThreadTest");
    List<String> dummyText = new List<string>()
    { "One", "Two", "Three", "Four", "Five", 
      "Six", "Seven", "Eight", "Nine", "Ten"};

    for (int i = 0; i < dummyText.Count; i++)
    {
        Console.WriteLine(dummyText[i]); // <-- No exception here
    }
}

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:13)

当您通过闭包将局部变量传递给线程或ThreadPool委托时,您需要复制变量。如:

for (int i = 0; i < dummyText.Count; i++)
{
    int index = i;
    Thread t = new Thread(() => PrintThreadName(dummyText[index]));
    // ...
}

如果你不这样做,那么变量基本上是通过引用传入的,并且索引将超过for循环末尾的数组边界(这可能在很久之前发生)关闭永远执行。)