Previous | Index | Next 

[PRB] Closing a form causes the ObjectDisposed exception

In some rare cases, when closing or unloading a form from inside an event handler in the migrated VB.NET project – for example, the Click event of a button – you get an ObjectDisposedException error.

The reason for this behavior may vary, but in a nutshell this error depends on the fact that VB Migration Partner’s library invokes a method or a property of the control that fired the event, but the control has now been disposed of together with its parent form.

If you observe this behavior, please send us a VB6 or .NET sample that exhibits this behavior, and we will fix it as soon as possible. In the meantime, however, you can adopt the fix described below. Let’s assume that you receive an ObjectDisposed exception when returning from the following error handler:

        Private Sub Button1_Click() Handles Button1.Click
            Unload6(Me)
        End Sub     ' << you get the exception after executing this line

Here’s the fix that solves this problem:

        Private Sub Button1_Click() Handles Button1.Click
            ' invoke the ReadyToUnload method when no other handlers are running
            AddHandler Application.Idle, AddressOf ReadyToUnload
        End Sub

        Private Sub ReadyToUnload(ByVal sender As Object, ByVal e As EventArgs)
            Unload6(Me)
        End Sub

 

Previous | Index | Next