6

Closing Form with Gif Launches InvalidOperationException

 2 years ago
source link: https://www.codesd.com/item/closing-form-with-gif-launches-invalidoperationexception.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Closing Form with Gif Launches InvalidOperationException

advertisements

This is clearly a problem of me not understanding how to properly setup a UI thread, but I can't figure out how to fix it.

I have a datagridview where I click a button, get the information from the network, and then display it on the datagridview with the new data. While it is on the network I have a form I show with an updating gif, a form I called "loading". Within that form I have the gif updating using the typical OnFrameChanged and m_isAnimating code that is on the internet.

However, no matter what format I use, I always get this exception caught here:

    Public loader As New Loading
    Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)

    Try  ' If animation is allowed call the ImageAnimator UpdateFrames method
        ' to show the next frame in the animation.
        Me.Invalidate()
        If m_IsAnimating Then
            ImageAnimator.UpdateFrames()
            Me.Refresh()
            'Draw the next frame in the animation.
            Dim aGraphics As Graphics = PictureBox1.CreateGraphics
             aGraphics.DrawImage(_AnimatedGif, New Point(0, 0))
                            aGraphics.Dispose()
        End If
    Catch ex As InvalidOperationException

    End Try
End Sub

And it usually says something along the lines of "was accessed from a thread it wasn't created on" or "Cannot access a disposed object. Object name: 'PictureBox'."

But I don't know why that is, since I am creating a new instance here every time. Here's the button's code:

 Private Sub btnSlowSearch_Click(sender As Object, e As EventArgs) Handles btnSlowSearch.Click

        Me.Cursor = Cursors.WaitCursor

        'get datatable

        loader.Show()
        BWorkerLoadProp.RunWorkerAsync() 'go get data on network
        'bworker will update datagridview with new data

        'wait for worker to finish
        If BWorkerLoadProp.IsBusy Then
            Threading.Thread.Sleep(1)
        End If

        loader.Close()
 End Sub

I realize it isn't very good code, but I have tried putting the loader inside the background worker, I have tried whatever. But no matter what the exception is called.

What's the proper way to show another updating form as I do background work?


The behavior documented is difficult to reproduce.
Probably something between the thread switching causes a call to OnFrameChanged after the call to close in the btnSlowSearch_Click.

In any case logic seems to suggest to call the ImageAnimator.StopAnimate in the close event of the form that shows the animation

So looking at your comment above I would add the following to your animator form

// Not needed
// Public loader As New Loading

Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
    Try
        Me.Invalidate()
        If m_IsAnimating Then
            ImageAnimator.UpdateFrames()
            Me.Refresh()
            'Draw the next frame in the animation.
            Dim aGraphics As Graphics = PictureBox1.CreateGraphics
             aGraphics.DrawImage(_AnimatedGif, New Point(0, 0))
                            aGraphics.Dispose()
        End If
    Catch ex As InvalidOperationException
       .. do not leave this empty or remove altogether
    End Try
End Sub

Private Sub Form_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

   ... if you need to stop the closing you should do it here without stopping the animation

   If m_IsAnimating Then
        ImageAnimator.StopAnimate(AnimatedGif, _
                      New EventHandler(AddressOf Me.OnFrameChanged)) 

        m_isAnimating = False
   End If
End Sub




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK