Delay Function
-
4. srpna 2012 21:59
Hi,
I want a delay function like this:
'Instruction 1
Delay (1500)
'Instruction 2
So instruction 1 is executed, then after 1500 ms (1.5 seconds), instruction 2 will be executed.
Thanks in advance.
Všechny reakce
-
4. srpna 2012 22:09
threading.thread.sleep(1500)
Be aware, if you perform this in the UI thread, the application freezes. Then use a Timer instead with interval=1500.
Armin
- Upravený Armin Zingler 4. srpna 2012 22:09
- Označen jako odpověď BGQQ 4. srpna 2012 23:10
-
4. srpna 2012 22:30
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim Tmr As New DelayTimer Tmr.Start() End Sub Class DelayTimer Inherits System.Windows.Forms.Timer Dim State As Integer Protected Overrides Sub OnTick(e As System.EventArgs) MyBase.OnTick(e) Select Case State Case 0 'instruction 1 Me.Interval = 1500 Me.State = 1 Me.Start() Case 1 'instruction 2 End Select End Sub End Class End Class
-
4. srpna 2012 22:58
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim Tmr As New DelayTimer Tmr.Start() End Sub Class DelayTimer Inherits System.Windows.Forms.Timer Dim State As Integer Protected Overrides Sub OnTick(e As System.EventArgs) MyBase.OnTick(e) Select Case State Case 0 'instruction 1 Me.Interval = 1500 Me.State = 1 Me.Start() Case 1 'instruction 2 End Select End Sub End Class End Class
Thanks to your reply; this works, but I want a function that could be used repetitively without repeating this code every time I want to make a delay. So I want a code like this:
Sub Delay(Byval Duration as Integer)
.
.
.
End Sub
-
4. srpna 2012 23:05
There is no way to do what you want on the UI thread. If you want your code on the UI thread, use a timer as I have shown, otherwise you can put your code on another thread and use sleep.Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim Tmr As New DelayTimer Tmr.Start() End Sub Class DelayTimer Inherits System.Windows.Forms.Timer Dim State As Integer Protected Overrides Sub OnTick(e As System.EventArgs) MyBase.OnTick(e) Select Case State Case 0 'instruction 1 Me.Interval = 1500 Me.State = 1 Me.Start() Case 1 'instruction 2 End Select End Sub End Class End Class
Thanks to your reply; this works, but I want a function that could be used repetitively without repeating this code every time I want to make a delay. So I want a code like this:
Sub Delay(Byval Duration as Integer)
.
.
.
End Sub
- Upravený JohnWeinMicrosoft Community Contributor 4. srpna 2012 23:06
- Označen jako odpověď BGQQ 4. srpna 2012 23:10
-
4. srpna 2012 23:11OK, Thank you all for your replies.
-
7. srpna 2012 2:22
You can add a timer to your form.
set the interval in the property to 1500
and from on your form double click on it
it will generate code for the subroutine
and put whatever you want to do in there