Reputation: 1474
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
Upvotes: 0
Views: 658
Reputation: 3393
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
Upvotes: 1
Reputation: 6375
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
Upvotes: 0