调用线程无法访问此对象(Dispatcher.CheckAccess)

时间:2019-09-20 07:59:45

标签: wpf vb.net serial-port

我试图在WPF中使用vb.net进行串行通信,而我唯一能做的成功就是发送数据。在接收数据时,我遇到了不同的错误,而这是我最难克服的错误。当我们想使用Control.InvokeRequired接收数据时,我尝试应用Windows窗体代码,但是由于这是WPF,因此已将其转移到Dispatcher.CheckAccess()。每次加载程序时,传输都可以,但是接收会给我System.InvalidOperation错误:“调用线程无法访问该对象,因为另一个线程拥有它。”我查看了其他解决方案,但没有一个起作用。

是否还有其他解决方案/过程可以从另一端接收数据?

我尝试了Windows文档和其他站点中的各种示例,但是在WPF应用程序中都没有成功。

Imports System.IO.Ports
Imports System.Text
Imports System.Threading
Imports System.Windows

Class MainWindow
    Private WithEvents serport As New SerialPort

    Private Delegate Sub SetTextCallback(ByVal [text] As String)

    Private Sub onLoad()
        Dim ports As String() = SerialPort.GetPortNames()
        Dim port As String

        For Each port In ports
            comPortCombo.Items.Add(port)
        Next port


    End Sub


    Private Sub ConButton_Click(sender As Object, e As RoutedEventArgs) Handles conButton.Click
        'serport = New SerialPort()

        If (comPortCombo.Text = "") Then
            MessageBox.Show("Please select COM port!")
            Exit Sub
        End If

        serport.PortName = comPortCombo.Text
        serport.BaudRate = baudCombo.Text

        serport.Open()


        transButton.IsEnabled = True
        conButton.IsEnabled = False
        disconButton.IsEnabled = True
    End Sub

    Private Sub DisconButton_Click(sender As Object, e As RoutedEventArgs) Handles disconButton.Click
        serport.Close()
        disconButton.IsEnabled = False
        transButton.IsEnabled = False
        conButton.IsEnabled = True
    End Sub

    Private Sub TransButton_Click(sender As Object, e As RoutedEventArgs) Handles transButton.Click
        serport.Write(transTextBox.Text & vbCrLf)
        transTextBox.Clear()

    End Sub


    Private Sub serport_datareceived(sender As Object, e As SerialDataReceivedEventArgs) Handles serport.DataReceived
        'recTextBox.Text = receiveserialdata()

        'with serForm's function???
        ReceivedText(serport.ReadLine())
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        If recTextBox.Dispatcher.CheckAccess() Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Dispatcher.Invoke(x, New Object() {(text)})

        Else
            recTextBox.Text &= [text]
        End If

    End Sub

    'Function receiveserialdata() As String
    '    ' receive strings from a serial port.
    '    Dim returnstr As String = ""
    '    Try
    '        Do
    '            Dim incoming As String = serport.ReadExisting()
    '            If incoming Is Nothing Then
    '                Exit Do
    '            Else
    '                returnstr &= incoming & vbCrLf
    '            End If
    '        Loop
    '    Catch ex As TimeoutException
    '        returnstr = "error: serial port read timed out."
    '    Finally
    '        If serport IsNot Nothing Then serport.Close()
    '    End Try

    '    Return returnstr
    'End Function

End Class

System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'

0 个答案:

没有答案