Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net MDI表单中的串行端口和控件更新_Vb.net_Serial Port - Fatal编程技术网

Vb.net MDI表单中的串行端口和控件更新

Vb.net MDI表单中的串行端口和控件更新,vb.net,serial-port,Vb.net,Serial Port,正如我的标题所暗示的,我有以下问题,我从串行端口接收数据,并使用control.invoke方法更新MDI表单中的richtextbox SerialPort.DataReceived事件中的代码 If myTerminal.Visible Then myTerminal.MyRichTextBox1.Invoke(New MethodInvoker(Sub()

正如我的标题所暗示的,我有以下问题,我从串行端口接收数据,并使用control.invoke方法更新MDI表单中的richtextbox

SerialPort.DataReceived事件中的代码

If myTerminal.Visible Then
            myTerminal.MyRichTextBox1.Invoke(New MethodInvoker(Sub()
                                                                   myTerminal.MyRichTextBox1.AppendText(dataLine & vbCrLf)
                                                               End Sub))
End If
但作为mdi表单,它具有关闭和重新打开的功能。因此,当serialport向richtextbox发送数据时,用户单击close按钮,表单就会被释放。在创建窗口句柄之前,无法对控件调用错误调用或BeginInvoke。。。。有什么想法吗

我的问候,
Ribben

代码不在SerialPort.DataReceived事件中,而是在事件处理程序中。是的,我在吹毛求疵,但它指出了一个解决办法。最好是让拥有myTerminal的表单在创建时添加处理程序,在关闭时删除处理程序。

谢谢您的回答,但不幸的是,这不是解决方案。首先,我的SerialPort类必须通知带有richtextbox的2个表单表单、带有Listview的表单和另一个负责绘制关于4个表单的非托管Directx 9.0c的类,因此为了正确实现SerialPort类,我创建了自己的事件。再次提到问题,这是因为Serialport.DataReceived每次出现都会在线程池中创建一个线程,当我处理表单时,它的速度太慢,无法赶上所有线程,因此至少有一个线程调用已经处理的控件

作为临时解决方案,我在TerminalForm类中提出了以下代码,该类继承了表单:

Private VisibleBoolean As Boolean = False
Private Index As Integer = 0

Private Sub DataToAppend(ByVal _text As String)
    If VisibleBoolean Then
        Me.MyRichTextBox1.Invoke(New MethodInvoker(Sub()
                                                       Me.MyRichTextBox1.AppendText(_text & vbCrLf)
                                                   End Sub))

    ElseIf Index = 1 Then
        Index = 0
        myDispose()
        RemoveHandler myserialport.DataToSend2, AddressOf DataToAppend

    End If
End Sub

Private Sub Me_Activated(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Activated
    VisibleBoolean = True
    AddHandler myserialport.DataToSend2, AddressOf DataToAppend
End Sub

Private Sub myDispose()
    If Index = 0 And Not Me.IsDisposed Then
        Me.Invoke(New MethodInvoker(Sub()
                                        MyBase.Dispose(True)
                                    End Sub))
    End If
End Sub

Protected Overrides Sub Dispose(ByVal disposing As Boolean)

End Sub

Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)

    Index = 1
    VisibleBoolean = False

End Sub
我知道我两个都不喜欢,但至少它起作用了! 任何其他的改进或建议都更重要