简体   繁体   中英

C# WCF service out of bounds array index

So I have a service that instantiates an array of objects and then starts a new process for those objects. I wait for the instantiation and the new processes to start before getting some data from the objects in a another thread. For some reason I am getting out bounds index when trying to read the data. It seems to be trying to access an index that is 1 larger than what i define (trying to access Os[4] when the last index is Os[3]), hence being out of bounds? what could be causing this?

        lock (initlock)
        {
            for (threadNum = 0; threadNum < 4; threadNum++)
            {
                Os[threadNum] = new myO();
                Thread TOs[threadNum] = new Thread(new ThreadStart(Os[threadNum].ProcessData));
                TOs[threadNum].Start();
            }
        }

        lock (initlock)
        {
            for (int i = 0; i < 4; i++)
            {
                Thread rd[i] = new Thread(new ThreadStart(() => Os[i].GetData());
                rd[i].Start();
            }
        }
for (int i = 0; i < 4; i++)
{
    int index = i;
    Thread rd[index] = new Thread(new ThreadStart(() => Os[index].GetData());
    rd[index].Start();
}

More in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM