Index was out of range c#

user3420235

Why i get index was out of range? I watched my code many time, and I cannot find where is mistake. Here is list:

 List<PictureBox> bullets = new List<PictureBox>();
 int i=0;

Event KeyDown:

void Tank_KeyDown(object sender, KeyEventArgs e)
{

        if (e.KeyData== Keys.space)
            {
            if (bullets.Count==0)
                {
                    PictureBox bullet = new PictureBox();
                    kulka.Name = Convert.ToString(i);
                    kulka.Image = new Bitmap("foto/kulka2.png");
                    Random dd = new Random();
                    if (pagr.Name == "RotateW")
                        bullet.Location = new Point(pagr.Location.X + 16, pagr.Location.Y - 8);
                    if (pagr.Name == "RotateS")
                        bullet.Location = new Point(pagr.Location.X + 16, pagr.Location.Y + pagr.Height + 2);
                    if (pagr.Name == "RotateA")
                        bullet.Location = new Point(pagr.Location.X - 8, pagr.Location.Y + 16);
                    if (pagr.Name == "RotateD")
                        bullet.Location = new Point(pagr.Location.X + pagr.Height + 11, pagr.Location.Y + 16);
                    bullet.Width = 5;
                    bullet.Height = 8;
                    bullet.SizeMode = PictureBoxSizeMode.StretchImage;
                    bullet.BringToFront();
                    bullets.Add(bullet);
                    panelis.Controls.Add(bullets[i]);
                    Task task = new Task(() => this.bullet_move(i, pagr.Name));
                    task.Start();
                    this.Refresh();
                }
            }
}

And here is my task:

void bullet_move(int j,string rotation)
    {
        while (true)
        {
                    if (rotation == "RotateW")
                    {
                            BeginInvoke(new Action(() => bullets[j].Top = bullets[j].Top - 1));

                        if (bullets[j].Top < 2)
                        {
                            BeginInvoke(new Action(() => panelis.Controls.Remove(bullets[j])));
                            bullets.RemoveAt(j);
                            Application.DoEvents();
                            i = 0;
                            break;
                        }
                    }
           } 
      }

Sometimes I get here index out of range:

BeginInvoke(new Action(() => bullets[j].Top = bullets[j].Top - 1));

and sometimes here:

  BeginInvoke(new Action(() => panelis.Controls.Remove(bullets[j])));

Why I get that error? :/ Thanks for help... :)

Dweeberly

I would start by replacing the BeginInvoke with Invoke. The BeginInvoke is an asynchronous call and you are making it in an extremely tight loop. The calls may not be executed in the order that they are made. This will make the code pretty much impossible to debug.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related