Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a do until loop that changes an image every 10 seconds. This will be adjusted but set only for 10 for testing purposes. I have 4 images (1.jpg-4.jpg) Theoretically it should change photo to 1st photo, wait 10 seconds, change to 2nd photo, wait 10 seconds and so on but no photo is displayed. I removed the threading and the last image is displayed.


VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim picNum As Integer = 0

    'Start Displaying images
    Do Until picNum = 4
        picNum = picNum + 1
        mainpbx1.ImageLocation = "F:\Programs\TV\" & picNum & ".jpg"
        Threading.Thread.Sleep(10000)
    Loop
End Sub


What I have tried:

I tried doing a Timer( I always use threading for stuff like this) and couldn't get it to be right. I had Timer enabled with 8 second interval but it didn't seem to be waiting after it Timer1.start() 
Posted
Updated 23-Oct-19 5:49am
v3

Using Thread.Sleep causes the UI thread to freeze. It cannot process any messages, which means it cannot repaint itself to show the selected image.

Use a Timer to update your images instead of a loop:
Timer Class (System.Windows.Forms) | Microsoft Docs[^]
VB.NET
Private picNum As Integer
Private picTimer As System.Windows.Forms.Timer

Private Sub ShowCurrentPicture()
    mainpbx1.ImageLocation = "F:\Programs\TV\" & picNum & ".jpg"
End Sub

Private Sub ShowNextPicture()
    picNum = picNum + 1
    ShowCurrentPicture()
    If picNum = 4 Then 
        picTimer.Enabled = False
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If picTimer Is Nothing Then
        picTimer = New System.Windows.Forms.Timer()
        AddHandler picTimer.Tick, AddressOf ShowNextPicture
        picTimer.Interval = 10000
    End If
    
    If Not picTimer.Enabled Then
        picNum = 1
        ShowCurrentPicture()
        picTimer.Enabled = True
    End If
End Sub
 
Share this answer
 
Comments
Cody O'Meara 24-Oct-19 12:01pm    
Worked Perfectly. Thank you!
Ooh. This can be tricky if you're not used to multi-threading winforms. There are some gotcha's that we've all hit in our time.

So here's the problem: You have 1 thread that draws the GUI. You are creating another thread to change some property for the first thread to draw. Only problem is, the first thread doesn't know about it. You changed it on another thread and don't tell the first.

Here's a nice way of implemented a threaded process that tells the first thread that something changes.


Updating Your Form from Another Thread without Creating Delegates for Every Type of Update[^]

if you get stick then just let us know.

Good luck ^_^
 
Share this answer
 
Comments
Dave Kreskowiak 23-Oct-19 11:53am    
He's not creating a background thread in his code. He's just putting the UI thread to sleep for 10 seconds and wondering why it doesn't update the picturebox.
Andy Lanng 23-Oct-19 12:23pm    
OOK - ok, but he should background it ^_^
Nah, your right. Maybe someone will find the link useful looking at this post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900