有人在使用ThreadPool时可以解释这种奇怪的行为吗?

Tes*_*101 4 c# multithreading threadpool

代码

using System;
using System.Threading;

public delegate void LoadingProgressCallback(double PercentComplete,string ItemName);
public delegate void LoadCompleteCallback(int ItemID, string ItemName);

public static class Program
{
    public static void Main(string[] args)
    {
        LoadTest loadTest = new LoadTest();
        loadTest.LoadItems(args);
    }
}

public class LoadTest
{       
    ManualResetEvent resetEvent;
    int numThreads = 0;

    public LoadTest()
    {}

    public void LoadItems(string[] Items)
    {
        numThreads = 0;
        resetEvent = new ManualResetEvent(false);

        foreach(string item in Items)
        {
            Console.WriteLine("Adding {0} to ThreadPool",item);
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                    Load(item, this.progCall, this.compCall);
                }
            );
            numThreads++;

            Thread.Sleep(100);//Remove this line

        }
        resetEvent.WaitOne();
    }

    public void progCall(double PercentComplete, string ItemName)
    {
        Console.WriteLine("{0}: is {1}% Complete [THREAD:{2}]",ItemName,PercentComplete.ToString(),Thread.CurrentThread.ManagedThreadId.ToString());
    }
    public void compCall(int ItemID, string ItemName)
    {
        Console.WriteLine("{0}: is Complete",ItemName);
        numThreads--;
        if(numThreads == 0)
        {
            resetEvent.Set();
        }
    }

    public void Load(string Item, LoadingProgressCallback progressCallback, LoadCompleteCallback completeCallback)
    {
        Console.WriteLine("Loading: {0} [THREAD:{1}]",Item,Thread.CurrentThread.ManagedThreadId.ToString());

        for(int i = 0; i <= 100; i++)
        {
            if(progressCallback != null)
            {
                progressCallback((double)i, Item);
            }
            Thread.Sleep(100);
        }
        if(completeCallback != null)
        {
            completeCallback(0,Item);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

意见

如果我从命令行运行这个程序,就像这样......

>TheProgram item1 item2

输出将如下所示.

将item1添加到ThreadPool中
:item1 [THREAD:3]
item1:0%完成[THREAD:3]
将item2添加到ThreadPool中
:item2 [THREAD:4]
item2:0%完成[THREAD:4]
item1:是1 %完成[THREAD:3]
item2:1%完成[THREAD:4]
item1:2%完成[THREAD:3]
item2:2%完成[THREAD:4]

但是,如果我删除此行.

Thread.Sleep(100);//Remove this line

从该LoadItems方法,输出看起来像这样.

将item1添加到ThreadPool
将item2添加到ThreadPool中
:item2 [THREAD:4]
正在加载:item2 [THREAD:3]
item2:0%完成[THREAD:4]
item2:0%完成[THREAD:3]
item2:是1 %完成[THREAD:4]
item2:1%完成[THREAD:3]
item2:2%完成[THREAD:3]
item2:2%完成[THREAD:4]

问题

看起来似乎使用了两个线程,尽管它们似乎都在使用相同的数据.为什么代码的行为如此?

Mar*_*ers 8

你正在关闭循环变量,这会给你一个意想不到的结果.试试这个:

foreach(string item in Items)
{
    string item2 = item;
    Console.WriteLine("Adding {0} to ThreadPool", item2);
    ThreadPool.QueueUserWorkItem
    (
        delegate
        {
            Load(item2, this.progCall, this.compCall);
        }
    );
    numThreads++;

    Thread.Sleep(100);//Remove this line

}
Run Code Online (Sandbox Code Playgroud)

参考