views:

64

answers:

0

Hi,

I've got two pictureboxes in a windows form which are assigned a new image in a separate thread, then are made visible to the windows form. So if we had two pictureboxes, A and B, if A is visible with an image on, B is invisible and a bitmap is loaded from a file and is assigned on the image property of B on a separate thread, then becomes visible on another thread that waits on the thread that is loading and assigning the image to the picture boxes. I've got a System.Timers.Timer that every 50 milliseconds checks if it is time to toggle the pictureboxes.

It all goes well but 20 minutes later the picture boxes don't become visible. According to the debug log file, images are loaded into the picture box, but only after about 2 minutes will an empty picturebox become visible. That's according to the debug log file, because in reality no picturebox is showing again.

Before assigning another image to an existing picture box, I dispose and clear references of the old one with this code:

if (pictureBox.Image != null)
{
  Image disposableImage = pictureBox.Image;
  disposableImage.Dispose();
  disposableImage = null;
  pictureBox.Image = null;
}

Immediately afterwards an image on the disk is decrypted and loaded into the picturebox from a memory stream which is then closed and disposed. I'm not sure if the decryption overhead has something to do with all this but the fact remains that the app works well for 20 minutes religiously before it stops showing images.

I put an extra debug code after the code that loads the image into the picturebox and that seems to have fixed it. Also there's extensive locking of global variables as there are two threads at play. Could the extra debug code have put in some delay that fixed it? Still this is only a gamble and does not guarantee that it will work at all times.

Thanks in advance