2016-02-19 64 views
0

我正在做一個異步子例程來繼續監聽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 

我很期待的解決方案。謝謝。

+0

你能告訴我們你的代碼使用重新形成的? –

回答

0

聽起來好像你在某個時候執行了checkInOut.Close,後來在你的代碼中試圖重新打開它。

.close方法關閉窗體並標記爲處置。如果你想繼續使用的形式,而不是

checkInOut.Close 

使用

checkInOut.Hide