串行端口上的线程

时间:2018-08-16 16:21:51

标签: vb.net multithreading serial-port

下面是我的代码,它在一定程度上有效。不幸的是,它一直间歇性地锁定。我以为如果我在单独的线程中运行可能会有所帮助,但我只是在猜测。我有点迷路。任何更好的解决方案将不胜感激。

Private Sub SetTextScale(ByVal gText As String)
    Try
        If Me.txtOutPut.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetTextScale)
            Me.BeginInvoke(d, New Object() {gText})
                      txtOutPut.Text = gText  

            Else
                gText = Mid(gText, 7, 6)
            End If
            If IsNumeric(gText) Then
                txtGrossKg.Text = gText : gText = ""
                Call CalcNet()
            End If
        Else
            gText = Mid(gText, 7, 6)
                txtOutPut.Text = gText
           End If
    Catch
    End Try

End Sub

1 个答案:

答案 0 :(得分:0)

这是完整的代码

    Try
        If Me.txtOutPut.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetTextScale)
            Me.BeginInvoke(d, New Object() {gText})
            If gLocal = 1111 Then
                If Mid(gText, 1, 2) = "et" Then gText = "" : Exit Sub
                ' ListBox1.Items.Add(gText)
                ' If ListBox1.Items.Count > 30 Then ListBox1.Items.Clear()
                txtOutPut.Text = gText : gStr1 = gText
                If InStr(gStr1, "+") Then
                    gInt1 = InStr(gStr1, "+")
                    gInt2 = InStr(gStr1, ".")
                    gStr2 = Mid(gStr1, gInt1 + 1, gInt2 - (gInt1 + 1)) & Mid(gStr1, gInt2, 3)
                    gText = gStr2
                End If
            Else
                gText = Mid(gText, 7, 6)
            End If
            If IsNumeric(gText) Then
                txtGrossKg.Text = gText : gText = ""
                Call CalcNet()
            Else
                gText = ""
            End If
        Else
            If gLocal <> 1111 Then
                'ListBox2.Items.Add(gText)
                'If ListBox2.Items.Count > 30 Then ListBox1.Items.Clear()

                gText = Mid(gText, 7, 6)
                txtOutPut.Text = gText
                If IsNumeric(gText) Then txtGrossKg.Text = gText : Call CalcNet()
            End If
            Call CalcNet()
            gText = ""
        End If
    Catch
    End Try