vb.net - 跨线程操作无效

标签 vb.net multithreading serial-port

我对一个非常简单的代码有疑问

Dim temperature As String = SerialPort1.ReadLine.ToString

        If temperature.StartsWith("temp") Then

            templab.Text = temperature.Substring(4, 2) & "°C"
        End If

它给了
Cross-thread operation not valid: Control 'templab' accessed from a thread other than the thread it was created on.

关于设置标签的文本

我确实在互联网上搜索了这个,我找到了一些解决方案,但我的问题不是这个,我过去用 Visual Studio 2008 和 Windows 7 编写了相同的代码,它没有给我任何错误。现在我使用 Windows 8 和 Visual Studio 2012。我如何在不调用的情况下解决这个问题?提前致谢

完整代码:
Public Class Form1


    Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim temperature As String = SerialPort1.ReadLine.ToString

        If temperature.StartsWith("temp") Then

            templab.Text = temperature.Substring(4, 2) & "°C"
        End If
        ' My.Computer.Audio.Play("C:\Users\Chris\Desktop\ROMANTIC MUSIC INSTRUMENTAL, PIANO AND SAXOPHONE.wav", AudioPlayMode.Background)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SerialPort1.Open()
    End Sub
End Class

最佳答案

您将需要在线程上调用您的方法。首先检查使用

templab.InvokeRequired

创建方法或匿名委托(delegate)并在控件上调用它。

来自 MSDN

对 Windows 窗体控件进行线程安全调用
查询控件的 InvokeRequired 属性。
如果 InvokeRequired 返回 true,则使用对控件进行实际调用的委托(delegate)调用 Invoke。
如果 InvokeRequired 返回 false,则直接调用控件。
在下面的代码示例中,线程安全调用在 ThreadProcSafe 方法中实现,该方法由后台线程执行。如果 TextBox 控件的 InvokeRequired 返回 true,则 ThreadProcSafe 方法会创建一个 SetTextCallback 实例并将其传递给窗体的 Invoke 方法。这会导致在创建 TextBox 控件的线程上调用 SetText 方法,并且在此线程上下文中直接设置 Text 属性。
' This event handler creates a thread that calls a  
' Windows Forms control in a thread-safe way. 
 Private Sub setTextSafeBtn_Click( _
 ByVal sender As Object, _
 ByVal e As EventArgs) Handles setTextSafeBtn.Click

     Me.demoThread = New Thread( _
     New ThreadStart(AddressOf Me.ThreadProcSafe))

     Me.demoThread.Start()
 End Sub 


' This method is executed on the worker thread and makes 
' a thread-safe call on the TextBox control. 
 Private Sub ThreadProcSafe()
   Me.SetText("This text was set safely.")
 End Sub


' This method demonstrates a pattern for making thread-safe 
' calls on a Windows Forms control.  
' 
' If the calling thread is different from the thread that 
' created the TextBox control, this method creates a 
' SetTextCallback and calls itself asynchronously using the 
' Invoke method. 
' 
' If the calling thread is the same as the thread that created 
' the TextBox control, the Text property is set directly.  

 Private Sub SetText(ByVal [text] As String)

 ' InvokeRequired required compares the thread ID of the 
 ' calling thread to the thread ID of the creating thread. 
 ' If these threads are different, it returns true. 
  If Me.textBox1.InvokeRequired Then 
     Dim d As New SetTextCallback(AddressOf SetText)
     Me.Invoke(d, New Object() {[text]})
 Else 
     Me.textBox1.Text = [text]
 End If 
End Sub

here举个例子和引用。

关于vb.net - 跨线程操作无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16124839/

相关文章:

c++ - boost::asio::serial_port 和 RTS 引脚

asp.net - json字符串从vb.net端到asp.net

vb.net - 在 VB.NET 中循环遍历字符串中的字符

java - 在Java中拆分文件以进行多线程

C - 多线程计数字母频率导致内存错误

c++ - SDL多线程

javascript - Chrome.serial API - 通过串口读取正确的字节数

python - 奇怪的 (py) 串行 linux 损坏

vb.net - vb.net 中的多行文本区域

vb.net - 跨线程操作无效