等待,直到计时器停止继续
我有一个在Visual Basic 2010中模拟*的程序。等待,直到计时器停止继续
首先,我从1到9生成3个随机数,并且因为我想模拟“旋转”,我决定经历一个循环,其中*的图像结果和事物出现在屏幕上。这个循环结束后,用户应该获得生成的1到9个数字的相应图片。
所以我看到最好的想法是设置一个计时器,例如把一个100的间隔,并启动它,并在每个滴答在屏幕上显示一些随机图像。
但是,似乎当我启动计时器时,它就像平行于它所调用的主函数。我不知道我是否在这里提供信息XD。 为自己更好看:
'CALCULATE WINNING RESULT
valor1 = GeneraAleatorio(1, 9) -> This custom function returns a random number
valor2 = GeneraAleatorio(1, 9)
valor3 = GeneraAleatorio(1, 9)
Timer1.Enabled = True
'NOW I PUT THE WINNING PICTURES THAT CORRESPOND WITH THE NUMBERS
ColocaImagen(1, valor1) -> Another custom made function, takes the position (1 to 3) and an image (1 to 9)
ColocaImagen(2, valor2)
ColocaImagen(3, valor3)
'END GAME
End() -> or whatever
我timer_tick功能是这样的:
If tiempo >= 4000 Then
Timer1.Enabled = False ' -> To make it stop when it reaches 4000 (4 seconds)
ElseIf tiempo <= 3900 Then
ColocaImagen(1, GeneraAleatorio(1, 9))
ColocaImagen(2, GeneraAleatorio(1, 9))
ColocaImagen(3, GeneraAleatorio(1, 9))
If tiempo >= ProgressBar.Minimum & tiempo <= ProgressBar.Maximum Then
ProgressBar.Value = tiempo
End If
tiempo = tiempo + 100 'Tiempo is "time" in Spanish, it increases 100 every 100ms
End If
看来,当我打电话timer1.enabled = true,则继续左右逢源:该程序进入蜱的功能,而且还可以在不等待计时器停止的情况下结束游戏。我想4秒通过,则显示正确的图片,并执行我想任何行动,或显示一个MsgBox什么
只需创建一个子程序,并调用它,当你的计时器达到4000看看是否有像这样会为你工作。
Public Class Form1
Dim valor1 As Integer
Dim valor2 As Integer
Dim valor3 As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Timer1.Enabled Then Exit Sub
'CALCULATE WINNING RESULT
valor1 = GeneraAleatorio(1, 9)
valor2 = GeneraAleatorio(1, 9)
valor3 = GeneraAleatorio(1, 9)
Timer1.Enabled = True
End Sub
Public Sub ShowFinalResult()
'NOW I PUT THE WINNING PICTURES THAT CORRESPOND WITH THE NUMBERS
ColocaImagen(1, valor1)
ColocaImagen(2, valor2)
ColocaImagen(3, valor3)
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Static tiempo As Integer
Timer1.Enabled = False
If tiempo >= 4000 Then
tiempo = 0
ShowFinalResult()
ElseIf tiempo <= 3900 Then
ColocaImagen(1, GeneraAleatorio(1, 9))
ColocaImagen(2, GeneraAleatorio(1, 9))
ColocaImagen(3, GeneraAleatorio(1, 9))
tiempo = tiempo + 100
If tiempo >= ProgressBar1.Minimum And tiempo <= ProgressBar1.Maximum Then ProgressBar1.Value = tiempo
Timer1.Enabled = True
End If
End Sub
End Class
试试这个:
虽然(Timer1.Enabled){} //将保持直到计时器结束为止
'CALCULATE WINNING RESULT
valor1 = GeneraAleatorio(1, 9) -> This custom function returns a random number
valor2 = GeneraAleatorio(1, 9)
valor3 = GeneraAleatorio(1, 9)
Timer1.Enabled = True
While (Timer1.Enabled) {} // Will keep blocking until the timer sets Enabled to false
'NOW I PUT THE WINNING PICTURES THAT CORRESPOND WITH THE NUMBERS
ColocaImagen(1, valor1) -> Another custom made function, takes the position (1 to 3) and an image (1 to 9)
ColocaImagen(2, valor2)
ColocaImagen(3, valor3)
'END GAME
End() -> or whatever
启动计时器不会导致程序停止并等待。
另外:
while循环会吃了CPU,而不在其内部睡眠 - pinkfloydx33
while循环会吃掉内部没有睡眠的CPU – pinkfloydx33 2013-02-24 00:39:16
好点,我会在我的答案中记下这一点。道具! – 2013-02-24 00:41:02
是的,这将是伟大的除了CPU使用:(我决定结束我的功能,当启动计时器,并在停止时继续在另一个 – fernandopcg 2013-02-24 19:31:59
谢谢!我决定遵循你的意见,并创建一个子,这取决于无论玩家是否赢得了胜利(也许以后我会改变它到一个点的方案)做一些行动或调用另一个子。 – fernandopcg 2013-02-24 17:22:53
@fernandopcg欢迎你,不胜感激 – 2013-02-24 19:58:56