【问题标题】:Threading on Serial port串行端口上的线程
【发布时间】:2018-08-20 16:33:38
【问题描述】:

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

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

【问题讨论】:

  • 当你必须调用 CalcNet() 时它会锁定吗?
  • 它在哪里锁定?
  • 您的ElseEnd If 语句数与您拥有的If 语句数不匹配。请更正此问题,因为它使代码很难解释。不过,我设法看到一件事:您在调用Me.BeginInvoke() 后立即访问txtOutput,这意味着您仍在后台线程中执行此操作。如果您在后台线程中,即InvokeRequired = True 时,您将无法访问任何 UI 元素。
  • 当我尝试执行 me.cmd 关闭按钮时它会锁定,
  • Calc et 只是将 no 加在一起,毛 - tare = net,所以那里没有锁定

标签: vb.net multithreading serial-port


【解决方案1】:

这是完整的代码

    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多