-
Basic Threading
Hey,
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:
Code:
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
}
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.
Any thoughts on what I need to be doing?
Thanks!
-
This will probably help you for how to handle threads with .NET applications:
http://msdn2.microsoft.com/en-us/lib...dd(VS.71).aspx
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.