title: Index was out of range c#
body: <p>Why i get index was out of range? 
  I watched my code many time, and I cannot find where is mistake.
  Here is list:</p>
  
  <pre><code> List&lt;PictureBox&gt; bullets = new List&lt;PictureBox&gt;();
   int i=0;
  </code></pre>
  
  <p>Event KeyDown:</p>
  
  <pre><code>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(() =&gt; this.bullet_move(i, pagr.Name));
                      task.Start();
                      this.Refresh();
                  }
              }
  }
  </code></pre>
  
  <p>And here is my task:</p>
  
  <pre><code>void bullet_move(int j,string rotation)
      {
          while (true)
          {
                      if (rotation == "RotateW")
                      {
                              BeginInvoke(new Action(() =&gt; bullets[j].Top = bullets[j].Top - 1));
  
                          if (bullets[j].Top &lt; 2)
                          {
                              BeginInvoke(new Action(() =&gt; panelis.Controls.Remove(bullets[j])));
                              bullets.RemoveAt(j);
                              Application.DoEvents();
                              i = 0;
                              break;
                          }
                      }
             } 
        }
  </code></pre>
  
  <p>Sometimes I get here index out of range:</p>
  
  <pre><code>BeginInvoke(new Action(() =&gt; bullets[j].Top = bullets[j].Top - 1));
  </code></pre>
  
  <p>and sometimes here:</p>
  
  <pre><code>  BeginInvoke(new Action(() =&gt; panelis.Controls.Remove(bullets[j])));
  </code></pre>
  
  <p>Why I get that error? :/
  Thanks for help... :)</p>