954,173 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to stop a loop displaying photos?

Hello.

I am writing an application that displays photos. I need to be able to stop the SlideShow when the user presses the "Stop" button. Right now, what's happening when the Stop button is pressed, is a public variable, bStop, is set to True. It works sort-of, and it also takes a long time. There must be a better way. Any ideas?

Thanks

Private Sub SlideShow()
        Dim x As Integer = 0
        With cboFileNames
            For x = 0 To (.Items.Count) - 1
                If bStop Then Exit For
                PictureBox1.Image = Image.FromFile(.Items(x).ToString)
                My.Application.DoEvents()
                Threading.Thread.Sleep(3000)
            Next
        End With
    End Sub
Sheryl99
Light Poster
27 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Hi,
Use Timer control instead of Thread.Sleep (). Because ur application will freeze for the given milliseconds.

if you use timer control, you can disable the timer when stop button is clicked.

To use timer, change the Following

> Use X as Global variable
> Draw a Timer Control and Set Enabled Property to True and Interval property to 3000
> Place the above code into Timer_Tick event

Dim x As Integer = 0

Private Sub Timer1_Tick ( ... )
   With cboFileNames 
      'For x = 0 To (.Items.Count) - 1
      'If bStop Then Exit For
       PictureBox1.Image =  Image.FromFile(.Items(x).ToString)
       My.Application.DoEvents()
      'Threading.Thread.Sleep(3000)
      'Next
      x = x + 1
  End With
End Sub

Private Sub ButtonStop_Click( .. )
   Timer1.Enabled = False
End Sub


Note : Also Check the x value whether it is inside upper and lower bound value inside TImer1_Tick Event

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You