Thread: Basic Threading

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    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!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM