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

1.5k views Asked by At

I have the following code for posting multiple HTTP Post requests to a web service from XML files. The problem here is if I put thread.Join() in the place where it is commented , I am able to put all the requests successfully . But if i use the second inner for loop for thread.Join() , i get

Index was outside the bounds of the array error

in

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

in files[x] . ie the main for loop . I am unable to get where i am going wrong . Please correct . I am using .NET4.0 CODE :

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();
        }
    }
}

}

1

There are 1 answers

0
Denis  Yarkovoy On BEST ANSWER

It happens because the expression () => function(files[x],p) is evaluated after the first inner loop is complete, and x is incremented in this loop. So you always get the out-of-range value of x=len.

To solve this, you need to declare another local variable and assign value of x to it, right before the anonymous function declaration, like this:

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

Here is a link to a more in-depth explanatio of why this happens: Can someone explain "access to modified closure" in C# in simple terms?