提问者:小点点

vb.net定时器逻辑(嵌套定时器)


我写了一个应用程序在VB.net,从我的PicoScope USB示波器捕获数据。

它使用计时器轮询范围,例如,它将每“x”秒轮询一次(这工作正常)。

然而,我也希望有一个选项,可以让这个“x”民意调查只运行“y”分钟,然后停止民意调查。

我在理解如何嵌套定时循环时遇到了问题(如果嵌套是正确的选择)。

这是我的代码;

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Counter = Counter + 1
        Label6.Text = Counter.ToString
        Call ReadScope()
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Counter = Counter + 1
        Label6.Text = Counter.ToString
        ' Call sub
        ' need to call routine below elsewhere
        ' I think this is where I need to loop timer 1

    End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click.

' THIS WORKS

 If y <= 0 Then
            ' here I want standard capture
            Call CaptureInterval()

        Else
            ' then here I want capture interval
            Call CaptureDuration()

        End If

End Sub

' AND THEN I HAVE (which works)
' This is the 'x' timer

Private Sub CaptureInterval()
        ' this works
        If x <= 0 Then
            MsgBox("Capture interval not set!")
            Return
        End If
        Me.Timer1.Interval = TimeSpan.FromSeconds(x).TotalMilliseconds
        Me.Timer1.Start()

    End Sub

' then I have the 'y' timer

   Private Sub CaptureDuration()
        ' this doesn't work
        MsgBox("Reached capture duration")
        ' might need a while timer2 here...
        Me.Timer2.Interval = TimeSpan.FromSeconds(y).TotalMilliseconds
        Me.Timer2.Start()
        ' Me.TimerStop()
        ' I think I need the code below in this loop.
        ' or just Call CaptureInterval() as below - but in this timer loop
        ' Call CaptureInterval()
' THIS IS WHERE I'M STUCK!
        If x < 1 Then
            MsgBox("Capture interval not set!")
            Return
        End If
        While Me.Timer1.Interval < y
            Me.Timer1.Interval = TimeSpan.FromSeconds(x).TotalMilliseconds
            Me.Timer1.Start()
        End While

    End Sub

Public Sub ReadScope()
' contains the processing to read the scope data
End Sub

先谢谢你。


共2个答案

匿名用户

试试这个例子 - 有一个带有单个计时器的表单,然后添加下面的代码。为了满足:

我还想要一个选项,可以让这个“x”民意调查只运行“y”分钟,然后停止民意调查。

你只需要设置_MaxTicks=(Y分钟*1000)/_TickInterval

Public Class Form1

    Private _MaxTicks As Integer = 10
    Private _TickCount As Integer = 0
    Private _TickInterval As Integer = 1000

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Timer1.Interval = _TickInterval
        AddHandler Me.Timer1.Tick, AddressOf TimerTick
        _TickCount = _MaxTicks
        Me.Timer1.Start()
    End Sub

    Private Sub TimerTick()
        Debug.Print(_TickCount)
        If _TickCount = 0 Then
            Me.Timer1.Stop()
            MessageBox.Show(String.Format("Time elapsed is roughly {0} ms", _MaxTicks * _TickInterval))
        End If
        _TickCount -= 1
    End Sub

End Class

匿名用户

我在计时器代码中添加了以下内容。

变量“y”是测试持续时间,我可以使用数字控件来选择。

如果它大于“0”,则If循环被激活,并且当计数器(IntervalTimer)与“y”匹配时将停止计时器。

   Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        IntervalTimer = IntervalTimer + 1
        Label6.Text = IntervalTimer.ToString

        If y >= 0 Then
            If IntervalTimer.ToString = y Then
                Me.Timer1.Stop()
            End If
        End If

        Call ReadScope()
    End Sub

请注意,最初发布的代码中的变量“Counter”已更改为“IntervalTimer”。