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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.