Gaurav Joseph
Gaurav Joseph

Reputation: 946

PictureBox continues loading after closing the form

I have a form(Images) that is called from another form(Main). The Images form uses a flow layout which holds together custom usercontrols. The usercontrol has a picturebox which uses the PictureBox.LoadAsync() method to get an image from a URI. The problem is that the network activity does not stop after closing the Images form.

I have tried handling the UserControl.Disposed event to do a PictureBox.CancelAsync() on the picturebox, but to no avail. What should I do so that the loading stops if the Images form has been closed.

Upvotes: 2

Views: 481

Answers (2)

It's possibly not the most clever solution, but this works for me:

Call this function from form_Closing event:

Private Sub closeformdisposingpictureboxes(f As Form)
    For Each c As Control In f.Controls
        If TypeOf c Is PictureBox Then
            Dim pbox As PictureBox = CType(c, PictureBox)
            pbox.Image.Dispose()
        End If
    Next
    f.Close()
    f.Dispose()
End Sub

Upvotes: 2

cyberj0g
cyberj0g

Reputation: 3797

Try adding 'PictureBox.CancelAsync()' to the form's Closed event handler. You have no control of when Disposed event will fire.

Upvotes: 0

Related Questions