簡體   English   中英

帶有循環的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中運行時,文本永遠不會出現在文本框中,但是當其他事件發生時,例如更改表單上的組合框將加載句子,如果我不斷更改組合,則文本框將每次更新。 此外,如果我在循環中添加一個msgbox,文本框將在每次關閉msgbox時更新。

我已經在Windows 8.1平板電腦上安裝了Visual Basic Express 2013,當在文本框命令的循環中放置一個停止時,它似乎沒有卡住並繼續循環,盡管文本框沒有在表單上更新。 我能看到的唯一其他問題是在Windows 7 PC上我看到以下輸出消息:

線程''(0x2e04)已退出,代碼為0(0x0)

雖然在Windows 8上我遇到:

線程''(0x149c)退出代碼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之后添加兩個System.Threading.Thread.Sleep(100)並且在子結束之前已經修復了問題,盡管我正在考慮將此代碼移動到后台工作者。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM