我对一个非常简单的代码有疑问
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