vb.net,Termie和线程以及“对象引用未设置为对象的实例”

时间:2016-10-26 22:29:22

标签: vb.net multithreading

我正在将Termie(Cite版本的Termite)的子集移植到vb.net。我在C#中使用过这个应用程序并对其进行了调整,以便为我成功完成一些工作。这一次,我需要将其折叠到vb.net应用程序中,当应用程序从串行端口接收时,我遇到“对象引用未设置为对象的实例”错误。

这是失败的代码

Private Sub ReadPort()

    While _keepReading
        If _serialPort.IsOpen Then
            Dim readBuffer As Byte() = New Byte(_serialPort.ReadBufferSize) {}
            Try
                ' If there are bytes available on the serial port,
                ' Read returns up to "count" bytes, but will not block (wait)
                ' for the remaining bytes. If there are no bytes available
                ' on the serial port, Read will block until at least one byte
                ' is available on the port, up until the ReadTimeout milliseconds
                ' have elapsed, at which time a TimeoutException will be thrown.
                Dim count As Integer = _serialPort.Read(readBuffer, 0, _serialPort.ReadBufferSize)
                Dim SerialIn As [String] = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count)
                DataReceived(SerialIn)

            Catch generatedExceptionName As TimeoutException
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            Dim waitTime As New TimeSpan(0, 0, 0, 0, 50)
            Thread.Sleep(waitTime)
        End If
    End While
End Sub`

DataReceived(SerialIn)抛出错误。

以下是它如何被指定为代表

 'begin Observer pattern
Public Delegate Sub EventHandler(param As String)
Public DataReceived As EventHandler
Public StatusChanged As EventHandler

我可能没有得到明显的东西但是线程创建和交互的各种风格对我来说都很模糊,而且我无法克服错误。

任何想法为什么会抛出无处不在的“对象引用未设置...”错误。是的,我确实得到了没有设置的对象。 ;)

以下是我试图打电话的内容:

Friend Delegate Sub StringDelegate(ByVal strData As String)

Public Sub ReceiveFromServer(ByVal strDataIn As String)

    If Me.InvokeRequired() Then
        ' InvokeRequired: We're running on the background thread. Invoke the delegate.
        Me.Invoke(New StringDelegate(AddressOf ReceiveFromServer), New Object() {strDataIn})
    Else
        Dim i As Integer = 0
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

由于完成最终胜过保持一致性,我继续重组了委托实现。现在,在串口上监听的例程如下所示:

 Public Delegate Sub DataReceived(ByVal MsgString As String)
Public Delegate Sub EventHandler(param As String)

(在其他地方使用EventHandler)

   Private Sub ReadPort()

    Dim MessagePump As DataReceived
    MessagePump = AddressOf Serial_Port_UI.ReceiveFromServer

    While _keepReading
        If _serialPort.IsOpen Then
            Dim readBuffer As Byte() = New Byte(_serialPort.ReadBufferSize) {}
            Try
                ' If there are bytes available on the serial port,
                ' Read returns up to "count" bytes, but will not block (wait)
                ' for the remaining bytes. If there are no bytes available
                ' on the serial port, Read will block until at least one byte
                ' is available on the port, up until the ReadTimeout milliseconds
                ' have elapsed, at which time a TimeoutException will be thrown.
                Dim count As Integer = _serialPort.Read(readBuffer, 0, _serialPort.ReadBufferSize)
                Dim SerialIn As [String] = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count)
                MessagePump.Invoke(SerialIn)
                'DataReceived(SerialIn)

            Catch generatedExceptionName As TimeoutException
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            Dim waitTime As New TimeSpan(0, 0, 0, 0, 50)
            Thread.Sleep(waitTime)
        End If
    End While
End Sub

这就产生了所需的字符串到目标Form / class中,如下所示:

   Public Delegate Sub StringDelegate(ByVal strData As String)

Public Sub ReceiveFromServer(ByVal strDataIn As String)

    If Me.InvokeRequired() Then
        ' InvokeRequired: We're running on the background thread. Invoke the delegate.
        Me.Invoke(New StringDelegate(AddressOf ReceiveFromServer), New Object() {strDataIn})
    Else
        Dim i As Integer = 0
    End If

End Sub

感谢汉斯的帮助。