睡眠定时器?

时间:2016-01-22 21:35:17

标签: vb.net

我经常环顾四周,找到了一些答案,但都没有。

我理解"睡眠()"冻结应用程序,这就是我添加计时器的原因。我想让它睡1000毫秒,所以我这样做了:

Timer1.Interval = 1000
Timer1.Start()

但是,这似乎不起作用。我没有错误,如果我没有计时器,程序就像运行一样运行。

我这样做了吗?如果没有,有人可以解决它吗? (计时器已启用)

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要收听Tick事件https://msdn.microsoft.com/en-us/library/system.windows.forms.timer.tick(v=vs.90).aspx

使用以下命令创建处理程序并跟踪刻度:

'prompts the user whether the timer should continue to run'
Private Shared Sub TimerEventProcessor(ByVal myObject As Object, _
                                       ByVal myEventArgs As EventArgs) _
                                   Handles myTimer.Tick
    myTimer.Stop()

    ' Displays a message box asking whether to continue running the timer. 
    If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _
                        MessageBoxButtons.YesNo) = DialogResult.Yes Then 
        ' Restarts the timer and increments the counter.
        alarmCounter += 1
        myTimer.Enabled = True 
    Else 
        ' Stops the timer.
        exitFlag = True 
    End If 
End Sub 

并在你的主要

Public Shared Sub Main()
    ' Adds the event and the event handler for the method that will 
    ' process the timer event to the timer. 

    ' Sets the timer interval to 5 seconds.
    myTimer.Interval = 5000
    myTimer.Start()

    ' Runs the timer, and raises the event. 
    While exitFlag = False 
        ' Processes all the events in the queue.
        Application.DoEvents()
    End While 

End Sub  

希望这有帮助。