Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 计时器作为睡眠?_Vb.net - Fatal编程技术网

Vb.net 计时器作为睡眠?

Vb.net 计时器作为睡眠?,vb.net,Vb.net,我环顾了很多地方,找到了一些答案,但都不管用 我知道“Sleep()”会冻结应用程序,这就是我添加计时器的原因。我想让它休眠1000毫秒,所以我这样做了: Timer1.Interval = 1000 Timer1.Start() 然而,这似乎不起作用。我没有收到任何错误,程序运行起来就像我没有计时器时一样 我这样做对吗?如果没有,有人能把它修好吗?(计时器已启用) 谢谢 您需要收听滴答声事件 创建一个处理程序并使用以下工具跟踪刻度: 'prompts the user whether the

我环顾了很多地方,找到了一些答案,但都不管用

我知道“Sleep()”会冻结应用程序,这就是我添加计时器的原因。我想让它休眠1000毫秒,所以我这样做了:

Timer1.Interval = 1000
Timer1.Start()
然而,这似乎不起作用。我没有收到任何错误,程序运行起来就像我没有计时器时一样

我这样做对吗?如果没有,有人能把它修好吗?(计时器已启用)


谢谢

您需要收听滴答声事件

创建一个处理程序并使用以下工具跟踪刻度:

'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  

希望这能有所帮助。

您需要收听滴答声事件

创建一个处理程序并使用以下工具跟踪刻度:

'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  


希望这有帮助。

Timer1
将每1000毫秒触发一次事件;他们不睡觉。你环顾四周时一定错过了这一点:你所做的就是启动计时器。您的代码将在
Timer1.Start()之后继续执行指令。您需要退出此方法,然后在
Timer1.Tick
事件中,您可以执行重新启动处理所需的操作。我认为您可能需要的是
等待任务。延迟(1000)
您是否查看了
线程。睡眠(1000)
Timer1
将每1000毫秒触发一次事件;他们不睡觉。你环顾四周时一定错过了这一点:你所做的就是启动计时器。您的代码将在
Timer1.Start()之后继续执行指令。您需要退出此方法,然后在
Timer1.Tick
事件中,您可以执行重新启动处理所需的操作。我认为您可能需要的是
等待任务。延迟(1000)
您是否查看了
线程。睡眠(1000)
?我知道这很容易翻译成VB,但是为了便于操作,你能用VB语法把它翻译成问号吗?所以这会让控制台等待,但同时让其他代码继续运行?我确实不确定你需要什么cenario,但是是的,所有其他工作线程和while循环中的任何东西都会继续运行。在切换变量之前,while循环之后的任何内容都不会运行。但在本例中,TimerEventProcessor()仅每5秒触发一次。while循环在等待计时器将
exitFlag
设置为
true
时,将使事情保持在原位,然后当退出标志发生变化时,它将执行while循环下的任何操作。如果您只想让事情无论如何都运行,则可以删除while循环。:)@弄糊涂了你能弄明白吗?你还有问题吗?我愿意更新我的答案,以更具体的解决方案,如果你能确切地澄清你试图用一个例子做什么?我知道这很容易翻译成VB,但是为了操作起见,你能把它放在VB语法中,在其中标记了问号吗?这样会让控制台等待,但同时让其他代码继续运行?我不确定你需要什么cenario,但是的,所有其他工作线程和while循环中的任何东西都将继续运行。在切换变量之前,while循环之后的任何内容都不会运行。但在本例中,TimerEventProcessor()仅每5秒触发一次。while循环在等待计时器将
exitFlag
设置为
true
时,将使事情保持在原位,然后当退出标志发生变化时,它将执行while循环下的任何操作。如果您只想让事情无论如何都运行,则可以删除while循环。:)@弄糊涂了你能弄明白吗?你还有问题吗?我愿意更新我的答案,以更具体的解决方案,如果你能确切地澄清你试图用一个例子做什么?