我在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
提前致谢。
答案 0 :(得分:0)
试试这个例子 - 有一个带有单个计时器的表单,然后添加下面的代码。为了满足:
我还想要一个选项,以便能够拥有这个&#39; x&#39;民意调查只适用于 &#39; Y&#39;分钟,然后停止投票。
您只需设置_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
答案 1 :(得分:0)
我在计时器代码中添加了以下内容。
变量&#39; y&#39;是测试持续时间,我可以使用数字控件选择。
如果它大于&#39; 0&#39;然后if循环被激活,并且当计数器(IntervalTimer)匹配&#39; 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
请注意变量&#34; Counter&#34;在最初发布的代码中已更改为&#34; IntervalTimer&#34;。