HTTP Post Request to web service . ERROR : Index was outside the bounds of the array

HTTP Post Request to web service . ERROR : Index was outside the bounds of the array

我有以下代码用于从 XML 文件向 Web 服务发送多个 HTTP Post 请求。这里的问题是,如果我把 thread.Join() 放在它被注释的地方,我就能够成功地提出所有的请求。但是如果我对 thread.Join() 使用第二个内部 for 循环,我会得到

Index was outside the bounds of the array error

thread[x] = new Thread(() => function(files[x],p));

在文件[x] 中。即主循环。我不知道我要去哪里错了。请改正 。我正在使用.NET4.0 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Xml;
using System.Net;
namespace ConsoleApplication4
{
    class Program
{
    int flag = 1;
    string destination;
    static void Main(string[] args)
    {

        int n = 0;
        Program p = new Program();
        Console.WriteLine("Enter the number");
        string s = Console.ReadLine();
        int.TryParse(s, out n);
        Console.WriteLine("Enter Destination");
        p.destination = Console.ReadLine();
        string path = "D:\temp";
        string[] files = null;
        files = Directory.GetFiles(path, "*.xml", SearchOption.TopDirectoryOnly);

        Thread[] thread = new Thread[files.Length];
        int x;
        int len = files.Length;
        for (int i = 0; i<len; i+=n)
        {
            x = i;

            for (int j = 0; j < n && x < len; j++)
            {
                thread[x] = new Thread(() => function(files[x],p));
                thread[x].Start();
                //thread[x].Join();
                x++;
            }
            int y = x - n;
            for (; y < x; y++)
            {
                int t = y;
                thread[t].Join();
            }

        }

        // thread[0] = new Thread(() => function(files[0]));
        //thread[0].Start();
        Console.ReadKey();

    }
    public static void function(string temp,Program p)
    {

        XmlDocument doc = new XmlDocument();
        doc.Load(temp);

        string final_d=p.destination + "response " + p.flag + ".xml";
        p.flag++;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.76.22.135/wpaADws/ADService.asmx");
        request.ContentType = "text/xml;charset=\"utf-8\"";
        request.Accept = "text/xml";
        request.Method = "POST";
        Stream stream = request.GetRequestStream();
        doc.Save(stream);
        stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
        {
            string soapResult = rd.ReadToEnd();
            doc.LoadXml(soapResult);
            File.WriteAllText(final_d, doc.DocumentElement.InnerText);

            //XmlTextWriter xml=new XmlTextWriter(
            Console.WriteLine(soapResult);
            //Console.ReadKey();
        }
    }
}

}

这是因为表达式 () => function(files[x],p) 第一个内部循环完成后被计算 ,并且 x 在此循环中递增。所以你总是得到 x=len.

的超出范围的值

为了解决这个问题,你需要在匿名函数声明之前声明另一个局部变量并为其赋值 x ,如下所示:

var localx=x;
thread[x] = new Thread(() => function(files[localx],p));

这里 link 更深入地解释了为什么会发生这种情况:Can someone explain "access to modified closure" in C# in simple terms?