XNA C#多线程导致ArgumentOutOfRangeException

时间:2014-03-28 21:01:19

标签: c# multithreading xna

我想利用多线程,因此我可以在模拟中拥有更多粒子。

public List<droplet> droplets = new List<droplet>();
private void collide(int indexOfA, int indexOfB)
{
    // collision code that only changes the position of droplet A and B
}

在更新方法中;

if (droplets.Count > 1)
{
    for (int i = 0; i < droplets.Count; i++)
    {
        for (int j = i; j < droplets.Count; j++)
        {
            Thread t = new Thread(() => collideParticles(i, j));
            t.Start();
        }
    }
}

我立即超出范围,当发生异常时通常为1或2。

我做错了什么,我该如何解决这个问题?

编辑:对于那些对碰撞方法有什么好处的人,请参见下文。

if (droplets[i].collisionRectangle.Intersects(droplets[j].collisionRectangle))
            {
                if (distanceBetween(droplets[i].position, droplets[j].position) < reactDistance * reactDistance)
                {
                    Vector2 sumVector = droplets[i].velocity + droplets[j].velocity;
                    Vector2 resultVector = sumVector / 2;
                    droplets[i].velocity = resultVector;
                    droplets[j].velocity = resultVector;

                    if (droplets[i].position.X > droplets[j].position.X) //x axis
                    {
                        droplets[i].velocity.X += repelSpeed;
                        droplets[j].velocity.X -= repelSpeed;

                        droplets[i].position.X -= -repelSpeed;
                        droplets[j].position.X += -repelSpeed;
                    }
                    else
                    {
                        droplets[i].velocity.X -= repelSpeed;
                        droplets[j].velocity.X += repelSpeed;

                        droplets[i].position.X += -repelSpeed;
                        droplets[j].position.X -= -repelSpeed;
                    }

                    if (droplets[i].position.Y > droplets[j].position.Y) //y axis
                    {
                        droplets[i].velocity.Y += repelSpeed;
                        droplets[j].velocity.Y -= repelSpeed;

                        droplets[i].position.Y -= -repelSpeed;
                        droplets[j].position.Y += -repelSpeed;
                    }
                    else
                    {
                        droplets[i].velocity.Y -= repelSpeed;
                        droplets[j].velocity.Y += repelSpeed;

                        droplets[i].position.Y += -repelSpeed;
                        droplets[j].position.Y -= -repelSpeed;
                    }
                }
            }

是的,这很乱。

1 个答案:

答案 0 :(得分:0)

我可以建议你使用线程池来做这种事情吗?分配一个线程并不便宜,你在渲染时可能会在内环中进行(哎哟!)。

我在这里看到的真正问题是,这里有记忆障碍,这意味着i&amp;的值。 j用于所有意图和目的是非确定性的。将collideParticles更改为打印功能:

    static void collideParticles(int i, int j)
    {
        Console.WriteLine("i = {0}, j = {1}", i, j);
    }

我得到以下的喷射:

i = 1, j = 3
i = 2, j = 3
i = 3, j = 3
i = 2, j = 3
i = 2, j = 3
i = 2, j = 3

将线程生成移动到collideParticles中会产生“预期”结果。

i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 1
i = 1, j = 2
i = 2, j = 2