0

I'm making simple game where I need to remove pictures after certain time without freezing everything else. I'm making explode event:

private void Explode(int x, int y)
{
  PictureBox explosion = new PictureBox();
  explosion.Image = Properties.Resources.explosion;
  explosion.SizeMode = PictureBoxSizeMode.StretchImage;
  explosion.Size = new Size(50, 50);
  explosion.Tag = "explosion";
  explosion.Left = x;
  explosion.Top = y;            
  this.Controls.Add(explosion);
  explosion.BringToFront();
}

I have already one timer for running the game, and i want to use if statement to remove picture when it lasts for 3 sec.

private void timer1_Tick(object sender, EventArgs e)
{
  foreach (Control x in this.Controls) 
  {
    if (x is PictureBox && x.Tag == "explosion")
    {
      if (EXPLOSION LASTS MORE THEN 3sec)
      {
        this.Controls.Remove(x);
      }
    }
  }
}

How can I do this?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Madley
  • 23
  • 4
  • Don't forget to dispose the control. – Reza Aghaei Jan 02 '20 at 20:55
  • 1
    I know this is not what you asked, but you are going to run into serious problems trying to use WinForm `PictureBox`es as sprites or graphic elements; performance will be terrible, layout and font scaling are going to screw you over like you will not believe, margins and padding are going to make alignment a nightmare, flickering will be uncontrollable. Save yourself days of frustration by doing this in UWP or using a toolkit with sprites built-in. – Dour High Arch Jan 02 '20 at 21:00
  • Instead of using a single timer for multiple picture boxes which may have different explosion timings, you can use async/await and `Task.Delay`. – Reza Aghaei Jan 02 '20 at 21:05

2 Answers2

2

Assuming you may have multiple picture box at the same time, then instead of using a single timer for multiple picture boxes which may have different explosion timings, you can use async/await and Task.Delay like this:

private async void button1_Click(object sender, EventArgs e)
{
    await AddExplodablePictureBox();
}
private async Task AddExplodablePictureBox()
{
    var p = new PictureBox();
    p.BackColor = Color.Red;
    //Set the Image and other properties
    this.Controls.Add(p);
    await Task.Delay(3000);
    p.Dispose();
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • @Madley Perhaps you forget to mark the answer as accepted/upvote it, otherwise let me know if you need more help about this answer. – Reza Aghaei Jan 26 '20 at 01:44
0

Before removing the picture box, of course it needs to release picturebox resources.

However there is problem when releasing. For more info, you can read more from the link below.

c# picturebox memory releasing problem

OO7
  • 660
  • 4
  • 10
  • Calling `Dispose` method of the `PictureBox` will be enough, you don't need to handle dispose of the image yourself, picture box will do it for you. – Reza Aghaei Jan 02 '20 at 21:34
  • And by the way, it's not the only point of the question, it also asks about *after time ...* – Reza Aghaei Jan 02 '20 at 21:35
  • By design, Dispose is used to release unmanaged resources, which are not track by garbage collector (gc). – OO7 Jan 02 '20 at 22:24