I have a loop and within that loop I’m running a Dispatcher Timer. Here’s my code:
Dim dt As New DispatcherTimer
dt.Interval = TimeSpan.FromSeconds(5)
AddHandler dt.Tick, AddressOf ChangeLabel
Dim items as new List(Of String) From {"Apple","Orange","Banana"}
For Each i In List
Me.Title = i
dt.StartTimer()
System.Threading.Thread.Sleep(5000)
Next
Private Sub ChangeLabel
'Show dt elapsed time in a label
End Sub
Basically what I want is to show each item in the items list pausing after each on for 5 seconds. I want to show elapsed time in a label and after the elapsed time hits 5 seconds show the next item. This is not possible using the Thred.Sleep method, because it just freezes my application for 5 seconds, and shows the next item without ever updating the label with elapsed time.
So what to use instead of Thread.Sleep to not freeze the application and update the label with elapsed time?
I’d start with
BackgroundWorker. Here’s something (in C#) to get you started.(Code should be quite similar in VB.)