I have a Windows Form with a PictureBox, and I want to change the image in the PictureBox every 10 seconds, to create a slide show effect. However, I also want the form to be able to respond to user's pressing buttons on the form. But if I just use a for loop, the whole form just freezes and the PictureBox never updates its picture. Here is the code I am using:
It seems that threading is needed to keep the form responsive while the loop is running, but I've given it a go and I just cannot get it working.
int i = 0; //Counter to decide which image to display next
for ( ;; ) //Loop forever
pictureBox1.Image = nextImage(i); //Get image i from database
this.Invalidate(); //Redraw the form to update the image
Thread.Sleep(10000); //Sleep for 10000ms = 10s
i++; //Increment the counter
Any thoughts on what I need to be doing?
This will probably help you for how to handle threads with .NET applications:
I would wager to say that you are probably right and you need a new thread to do what you're trying to do unless you have a .NET timer class that you can use. If you can cause an event to be triggered every 10 seconds from a timer class, and have the timer's thread call a function of your own, then you can let the timer class handle the business of threading.