vb.net 带循环的线程在 Windows 8.1 中不工作但在 Windows 7 中工作
vb.net thread with loop not working in Windows 8.1 but works in Windows 7
我正在开发一个 Windows 表单应用程序来与 Visual Basic 2010 express 中内置的 GPS 进行通信。我已经使用通过蓝牙连接的外部 GPS 在 Windows 7 上对其进行了测试,它工作正常,但我在 windows 8.1.
上遇到了问题
目前正在测试的特定元素将 GPS 中的 NMEA 语句输出到文本框中。之前的测试版本将未处理的数据包传递到文本框,在 windows 7 和 8.1
上都没有问题
下一个测试是拆分单个句子并将它们一个一个地添加到文本框中,这涉及循环遍历一组句子。
第二个测试在 windows 7 笔记本电脑上运行良好,但在 windows 8.1 平板电脑上运行良好。当 Windows 8.1 中的 运行 文本永远不会出现在文本框中,但是当发生其他事件(例如更改表单上的组合框)时,将加载一个句子,如果我继续更改组合,则文本框将更新每一次。此外,如果我在循环中添加一个消息框,则每次关闭消息框时文本框都会更新。
我已经在 Windows 8.1 平板电脑上安装了 Visual Basic Express 2013,当在文本框命令的循环中放置一个停止时,它似乎没有卡住并继续循环,虽然文本框不更新表格。
我能看到的唯一其他问题是,在 windows 7 PC 上,我看到以下输出消息:
The thread '' (0x2e04) has exited with code 0 (0x0)
我在 windows 8 遇到:
The thread '' (0x149c) has exited with code 259 (0x103)
我不确定代码 259 是否指向线程的问题。
下面是代码的主要部分
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class frmCommInit
Public GPSsettings As New GPSsetting
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String) 'Delegate added to provent threading areas during data recieval
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
cboPorts.Items.AddRange(myPort)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
RecievedText(SerialPort1.ReadExisting())
End Sub
Private Sub RecievedText(ByVal [text] As String)
Dim NMEASentances() As String = Split(text, vbCrLf)
Dim l As Integer
Dim i As Integer
If Me.rtbOutput.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbOutput.Text = text & vbCr & Me.rtbOutput.Text
For i = 0 To NMEASentances.Length - 1
l = NMEASentances(i).Length
If l > 10 Then
If NMEASentances(i)(0) = "$" And NMEASentances(i)(l - 3) = "*" Then
Me.rtbOutput.Text = NMEASentances(i) & vbCr & Me.rtbOutput.Text
End If
End If
Next
End If
End Sub
不确定为什么它在 windows 7 中运行良好,但在 windows 8.1 中运行不佳,但我发现在 else 之后和 then 结束之前添加两个 System.Threading.Thread.Sleep(100) sub 已经解决了问题,但我正在考虑将此代码移至后台工作人员。
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class frmCommInit
Public GPSsettings As New GPSsetting
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String) 'Delegate added to provent threading areas during data recieval
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
cboPorts.Items.AddRange(myPort)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
RecievedText(SerialPort1.ReadExisting())
End Sub
Private Sub RecievedText(ByVal [text] As String)
Dim NMEASentances() As String = Split(text, vbCrLf)
Dim l As Integer
Dim i As Integer
If Me.rtbOutput.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(text)})
Else
System.Threading.Thread.Sleep(100)
Me.rtbOutput.Text = text & vbCr & Me.rtbOutput.Text
For i = 0 To NMEASentances.Length - 1
l = NMEASentances(i).Length
If l > 10 Then
If NMEASentances(i)(0) = "$" And NMEASentances(i)(l - 3) = "*" Then
Me.rtbOutput.Text = NMEASentances(i) & vbCr & Me.rtbOutput.Text
End If
End If
Next
End If
System.Threading.Thread.Sleep(100)
End Sub
End Class
我正在开发一个 Windows 表单应用程序来与 Visual Basic 2010 express 中内置的 GPS 进行通信。我已经使用通过蓝牙连接的外部 GPS 在 Windows 7 上对其进行了测试,它工作正常,但我在 windows 8.1.
上遇到了问题目前正在测试的特定元素将 GPS 中的 NMEA 语句输出到文本框中。之前的测试版本将未处理的数据包传递到文本框,在 windows 7 和 8.1
上都没有问题下一个测试是拆分单个句子并将它们一个一个地添加到文本框中,这涉及循环遍历一组句子。 第二个测试在 windows 7 笔记本电脑上运行良好,但在 windows 8.1 平板电脑上运行良好。当 Windows 8.1 中的 运行 文本永远不会出现在文本框中,但是当发生其他事件(例如更改表单上的组合框)时,将加载一个句子,如果我继续更改组合,则文本框将更新每一次。此外,如果我在循环中添加一个消息框,则每次关闭消息框时文本框都会更新。
我已经在 Windows 8.1 平板电脑上安装了 Visual Basic Express 2013,当在文本框命令的循环中放置一个停止时,它似乎没有卡住并继续循环,虽然文本框不更新表格。 我能看到的唯一其他问题是,在 windows 7 PC 上,我看到以下输出消息:
The thread '' (0x2e04) has exited with code 0 (0x0)
我在 windows 8 遇到:
The thread '' (0x149c) has exited with code 259 (0x103)
我不确定代码 259 是否指向线程的问题。
下面是代码的主要部分
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class frmCommInit
Public GPSsettings As New GPSsetting
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String) 'Delegate added to provent threading areas during data recieval
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
cboPorts.Items.AddRange(myPort)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
RecievedText(SerialPort1.ReadExisting())
End Sub
Private Sub RecievedText(ByVal [text] As String)
Dim NMEASentances() As String = Split(text, vbCrLf)
Dim l As Integer
Dim i As Integer
If Me.rtbOutput.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbOutput.Text = text & vbCr & Me.rtbOutput.Text
For i = 0 To NMEASentances.Length - 1
l = NMEASentances(i).Length
If l > 10 Then
If NMEASentances(i)(0) = "$" And NMEASentances(i)(l - 3) = "*" Then
Me.rtbOutput.Text = NMEASentances(i) & vbCr & Me.rtbOutput.Text
End If
End If
Next
End If
End Sub
不确定为什么它在 windows 7 中运行良好,但在 windows 8.1 中运行不佳,但我发现在 else 之后和 then 结束之前添加两个 System.Threading.Thread.Sleep(100) sub 已经解决了问题,但我正在考虑将此代码移至后台工作人员。
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class frmCommInit
Public GPSsettings As New GPSsetting
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String) 'Delegate added to provent threading areas during data recieval
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
cboPorts.Items.AddRange(myPort)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
RecievedText(SerialPort1.ReadExisting())
End Sub
Private Sub RecievedText(ByVal [text] As String)
Dim NMEASentances() As String = Split(text, vbCrLf)
Dim l As Integer
Dim i As Integer
If Me.rtbOutput.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(text)})
Else
System.Threading.Thread.Sleep(100)
Me.rtbOutput.Text = text & vbCr & Me.rtbOutput.Text
For i = 0 To NMEASentances.Length - 1
l = NMEASentances(i).Length
If l > 10 Then
If NMEASentances(i)(0) = "$" And NMEASentances(i)(l - 3) = "*" Then
Me.rtbOutput.Text = NMEASentances(i) & vbCr & Me.rtbOutput.Text
End If
End If
Next
End If
System.Threading.Thread.Sleep(100)
End Sub
End Class