I've written an app in VB.net that captures data from my PicoScope USB oscilloscope.
It polls the 'scope using a timer, so for example it will poll every 'x' seconds (which is working fine).
However - I also want an option to be able to have this 'x' poll to only run for 'y' minutes, and then stop polling.
I'm having a problem understanding how to nest the timing loops (if nesting is the right option).
This is my code;
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
Thanks in advance.
Try this example - there is a form with a single timer and then add the code below. In order to satisfy:
I also want an option to be able to have this 'x' poll to only run for
'y' minutes, and then stop polling.
You just need to set _MaxTicks = (Y minutes * 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
I added the following to the timer code.
Variable 'y' is the test duration, which I can select using a numeric control.
If it's greater than '0' then the if loop is activated, and will stop the timer when the counter (IntervalTimer) matches '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
Please note that the variable "Counter" in the code posted originally has changed to "IntervalTimer".
Related
I would like to do a For Loop inside a timer. Since I want to check an array with element row continuously and I cant continue since the loop seems to not move from 0 on tick, is there a way around this?
I first tried this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For row = 0 To 9
msgbox(row)
Next
And then I tried another approach, as suggested in an answer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
row+=1
msgbox(row)
if row = 10 then
timer1.stop()
end if
Next
MsgBox will now output 0++ on tick but does not stop at 10.
Output Picture
Dim row As Integer = -1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RefreshData()
Timer1.Enabled = True
initialize()
End sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
increment()
MsgBox(row)
End sub
Private Sub increment()
If row > 8 Then
row = 0
Else
row = row + 1
End If
'my if else statement for checking array variable(row)
End Sub
With this code msgbox was able to output 0 to 9 and repeat the process since i need it to continuously monitor the array. For some reason though when msgbox is situated at the first line of increment method or on timer tick before increment is called the output stays -1 the whole time dunno why. Anyways thanks for all the input as im still new to visual basic 2015
Instead of using for loop use a global variable and increment it inside the tick function
Dim ctr as integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ctr=ctr+1
row(ctr)
msgbox(row)
if ctr == 10 then
ctr = 0
timer1.stop
end if
In the following code I have a timer that counts down from 5 mins. I am trying to have a visual count down timer in a lbl in mm:ss but the example I used doesn't work. It counts down but doesn't update the lbl until it hits 00:00.
The asker of the following question (were I got the code) said it works perfectly but for me it doesn't at all.
The Example I used
My code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
picLogo.SizeMode = PictureBoxSizeMode.StretchImage
'Timer until update
tmrUpdate.Interval = 300000 '5 minutes
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrUpdate.Enabled = True
End Sub
Private Sub tmrUpdate_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTimer.Text = ts.ToString("mm\:ss")
Else
lblTimer.Text = "00:00"
tmrUpdate.Stop()
End If
End Sub
Answer:
Using a Async Sub I had the count down timer running while other stuff was going on in the back ground. This way the app could still be used during the Sub Wait() and this code also displayed the count down timer.
Used one timer on a 1 sec interval.
Private Async Sub DoStuff()
'Doing stuff
timeUpDate = 599
tmrUpdate.Start()
Application.DoEvents()
Await Task.Run(Sub()
Wait()
End Sub)
Loop
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim hms = TimeSpan.FromSeconds(timeUpDate)
Dim m = hms.Minutes.ToString
Dim s = hms.Seconds.ToString
If timeUpDate > 0 Then
timeUpDate -= 1
lblTimer.Text = (m & ":" & s)
Else
tmrUpdate.Stop()
lblTimer.Text = "text"
End If
End Sub
Private Sub Wait()
Threading.Thread.Sleep(600000)
End Sub
I need a way to finding out when the time from 0 ends up as 4 secs.
My code is as follows:
I have a global variable called weightdelaycount, which is incremented every 1000 intervals.
Private Sub Timer_weightcheck_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer_weightcheck.Tick
weightdelaycount = weightdelaycount + 1
End Sub
Now I have the do while loop that runs infinitely and only stops depending on two conditions. Either the weightchange = True or if the clock = 4 secs.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
weightdelaycount = 0
Timer_weightcheck.Enabled = True
Do While 1
If (weightchange() = True ) Then
Timer_weightcheck.Enabled = False
Exit Do
End If
If (weightdelaycount = 4) Then
Timer_weightcheck.Enabled = False
Exit Do
End If
Loop
MessageBox.Show(weightdelaycount)
End Sub
From the routine above you see that I'm using exit Do to exit the loop if the two conditions are met. The problem is that if the weightchange() is not True and 4 seconds passed the systems doesn't stop. I can put a delay in there and then it works, but I need to be accurate with the values that I get for the weight from a scale. If I put a delay, then the values will not be accurate. Is there a way to solve this?
Thank you in advance
Instead of using a timer, you could use a stopwatch. This code will loop until 4000ms after the stopwatch has started. You'll never get it to stop exactly on 4000ms because of the time it takes to run through the the Do..Loop(minimal really), but I presume a couple of ms after is close enough.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim weightdelaycount = 0
Dim timer As New Stopwatch
timer.Start()
Do
If (weightchange() = True) Then
timer.Stop()
Exit Do
End If
Loop Until timer.ElapsedMilliseconds > 4000
timer.Stop()
MessageBox.Show(timer.ElapsedMilliseconds)
End Sub
I have a loop/for each here and I want to pause it for one hour after 25 repeats
Dim i As Integer
i = 0
For Each item In ListBox2.Items
i = i + 1
MessageBox.Show(item.ToString)
delay(1000)
Label5.Text = i
Next
Something like:
Dim i As Integer
Dim j As Integer
i = 0
j = 0
For Each item In ListBox2.Items
if(j!=25)
i = i + 1
MessageBox.Show(item.ToString)
delay(1000)
Label5.Text = i
j=j+1
else
delay(25*60*1000)
j = 0
Next
You should add a timer object to your form and set the interval to 3600000 (ms). Place your loop, that goes to 25 in a sub like Sub DoSomething(). In the Timer.Tick event handler you simply place another DoSomething() (and maybe stop the timer before DoSomething() and start the timer after, depending on how long DoSomething takes).
Then, when you want to start the task, call DoSomething() once manually and then start the timer. Every 60 minutes it will execute DoSomething().
This approach is much smoother, since your form will not be stuck in some loop or Thread.Sleep(). You should avoid this for any task that takes a significant amount of time.
It's furthermore easy to stop the task.
Public Class Form1
Private WithEvents TaskTimer As New System.Windows.Forms.Timer With {.Interval = 15000, .Enabled = False}
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
DoSomething()
End Sub
Private Sub DoSomething()
TaskTimer.Stop() 'Stop the timer while the task is being performed
For i = 1 To 25
MessageBox.Show("Hey ho yippieyahey")
Next
TaskTimer.Start() 'Restart the timer
End Sub
Private Sub TaskTimer_Tick(sender As Object, e As EventArgs) Handles TaskTimer.Tick
DoSomething()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
TaskTimer.Stop()
End Sub
End Class
I have a list of links stored in a listbox. I want a timer control to load one link after the other every 4 seconds, and once it's reached the end of the list, I want the timer to stop. I've tried to achieve this with the code below and it's only loading the first link. Can someone point out my mistake(s)? Thanks in advance!
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim counter As Integer = counter + 1
If counter > ListBox2.Items.Count Then
counter = 0
Timer1.Enabled = False
Else
Dim i As Integer
For i = 0 To ListBox2.Items.Count - 1
WebBrowser1.Navigate(ListBox2.Items(i))
Next
End If
End Sub
You need to declare your counter variable at the class level instead of within your Tick handler, or else its value is always reinitialized to 1.
So in other word, defined it oustide the handler.
Also, you must not loop the entire content of the ListBox within your tick handler.
Here's a better version:
Public Class Form1
Private counter As Integer = 0
...
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If counter > ListBox2.Items.Count - 1 Then
counter = 0
Timer1.Enabled = False
Else
WebBrowser1.Navigate(ListBox2.Items(counter))
End If
'Increment at the end to get element(0)
counter = counter + 1
End Sub
End Class
Cheers