无法访问已处置的对象

时间:2016-02-19 17:00:39

标签: vb.net

我正在做一个异步子程序来继续监听UDP客户端。当我收到消息时,我将使用BeginInvoke更新UI线程。但现在,当我重新打开表单时,我遇到了一个问题。它会给我一个例外 - 无法访问已处置的对象。对象名称:checkInOut。 checkInOut是我的表单名称。

Private Sub checkInOut_Load(sender As Object, e As EventArgs) Handles Me.Load
        FormSettings()
        udpClient.BeginReceive(AddressOf udpAsyncReceive, Nothing)
End Sub

Private Sub udpAsyncReceive(asyncResult As IAsyncResult)
    Try
        Dim remoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
        Dim receiveBytes As Byte() = udpClient.EndReceive(asyncResult, remoteIpEndPoint)
        Dim receiveMsg As String = Encoding.UTF8.GetString(receiveBytes)
        If Me.IsHandleCreated = False Then
            Me.CreateHandle()
        End If
        '' Pass the string to a method that runs on the UI thread
        Me.BeginInvoke(New Action(Of String)(AddressOf DataReceived), receiveMsg)
        '' Continue receiving
        udpClient.BeginReceive(AddressOf udpAsyncReceive, Nothing)
    Catch ex As Exception
        GeneralHelper.showExceptionErrorMsg(ex)
    End Try
End Sub

Private Sub DataReceived(receiveMsg As String)
    txtReservationID.Text = receiveMsg
End Sub

我使用菜单条调用openForm()并重新打开表单。

Private Sub CheckInOutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CheckInOutToolStripMenuItem.Click
    GeneralHelper.openForm(New checkInOut)
End Sub

openForm子例程

Public Sub openForm(ByVal formName As Form)

    If Form.ActiveForm.MdiChildren.Length > 0 Then
        For Each childForm In Form.ActiveForm.MdiChildren
            childForm.Close()
        Next
    End If

    formName.MdiParent = Form.ActiveForm
    formName.Show()
End Sub

我期待着解决方案。谢谢。

1 个答案:

答案 0 :(得分:0)

听起来好像在某些时候你在某个时候执行checkInOut.Close,然后在你的代码中尝试重新打开它。

.close方法关闭表单并将其标记为可处置。如果您想继续使用表单而不是

checkInOut.Close

使用

checkInOut.Hide