如何在不阻塞Visual Basic中的其余程序的情况下执行任务

问题描述 投票:-2回答:1

我目前正在研究用于汽车行业的LIN总线。我必须发送到这个总线5的信息,每个需要25ms去,我必须在每次发送之间放60ms。我目前正在使用一个按钮启动一个计时器,该计时器运行另一个计时器,每次更改信息并在它们之间设置延迟。问题是,当我按下按钮时,我无法再次按下按钮以停止计时器,因为程序太忙了。

我的代码:

    Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click

    If Start.Text = "START" Then
        Start.Text = "STOP"
        Timer6.Interval = 1000
        Timer6.Enabled = True
        Timer6.Start()
    ElseIf Start.Text = "STOP" Then
        Start.Text = "START"
        Timer6.Enabled = False
        Timer6.Stop()
    End If

End Sub

Dim auto As Integer = -1

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    auto += 1
    Select Case auto
        Case 0
            send("07")
        Case 1
            send("05")
        Case 2
            send("06")
        Case 3
            send("08")
        Case 4
            send("09")
    End Select
    If auto = 4 Then
        auto = -1
    End If
End Sub
Private Sub Timer6_Tick(sender As Object, e As EventArgs) Handles Timer6.Tick
    Dim n As Integer = 0
    Timer2.Interval = 100
    While n <> 5
        Timer2.Enabled = True
        Timer2.Start()
        n += 1
    End While
    If Start.Text = "STOP" Then
        Timer6.Interval = (freq.Text * 10 ^ 3)
        Timer6.Enabled = True
        Timer6.Start()
    ElseIf Start.Text = "START" Then
        Timer6.Enabled = False
        Timer6.Stop()
    End If
End Sub

你有一个解决方案,所以程序运行,我可以随时停止发送信息吗?

提前谢谢您的时间。

.net vb.net
1个回答
0
投票

您需要查看BackgroundWorker,我现在没有工作样本,但这应该指向正确的方向。

© www.soinside.com 2019 - 2024. All rights reserved.