如何在重复25次后暂停循环60分钟?

时间:2014-04-01 06:28:04

标签: vb.net visual-studio-2010 visual-studio loops visual-studio-2012

我在这里有一个循环/我想在25次重复后暂停一小时

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

2 个答案:

答案 0 :(得分:1)

类似的东西:

   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

答案 1 :(得分:0)

您应该在表单中添加一个计时器对象,并将间隔设置为3600000(ms)。放置循环,在Sub DoSomething()之类的子目录中为25。在Timer.Tick事件处理程序中,您只需放置另一个DoSomething()(并可能在DoSomething()之前停止计时器并在之后启动计时器,具体取决于DoSomething需要多长时间。) 然后,当您想要启动任务时,手动调用DoSomething(),然后启动计时器。每隔60分钟就会执行DoSomething()。

这种方法更顺畅,因为你的表单不会卡在某个循环或Thread.Sleep()中。对于任何需要花费大量时间的任务,您应该避免这种情况。 它更容易停止任务。

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