|
-
May 23rd, 2004, 05:47 PM
#1
Thread Starter
Frenzied Member
loops
I know how to make something loop for a fixed number of times(10 for example) but, I would like to know how to make something loop for a minute or a few seconds. I know you would probably use the Timer control but im not sure how. Can someone help me?
-
May 23rd, 2004, 06:36 PM
#2
Sleep mode
Place a label and a timer and set its interval to 1000 and enabled property to True .
VB Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Timer1.Tick
For i As Integer = 0 To 10
Me.Label1.Text += i.ToString
Next
End Sub
-
May 23rd, 2004, 07:31 PM
#3
yay gay
Hold in a (date) variable the current system time. Then just do a function that will subtract your saved date to System.DateTime.Now (theres a method inside it called .Subtract()) and it will return a TimeSpan structure (if I am not wrong).
\m/  \m/
-
May 24th, 2004, 05:29 AM
#4
PowerPoster
Originally posted by Pirate
Place a label and a timer and set its interval to 1000 and enabled property to True .
VB Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Timer1.Tick
For i As Integer = 0 To 10
Me.Label1.Text += i.ToString
Next
End Sub
In the above you need to set the Timer.Interval to 10000 to get a 10 second delay and the loop simply changes Label1.Text millions of times a second.
If you are trying to delay the programme execution for 10 seconds then you should originally set the Timer1.Enabled property to False, Timer1.Interval to 1000, then when you want to start the delay set Timer1.Enabled to True. The Timer1_Click event should contain the following code: (having first declared I as a form scope Integer)
VB Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
I += 1
If I = 10 Then
Timer1.Enabled = False
I = 0
End If
End Sub
P T Exorcist's suggestion also works OK but to clarify it use the following code:
VB Code:
Private Sub Delay10()
Dim time1 As Date = Now
Do
If Now > time1.AddSeconds(9) Then Exit Do
Loop
End Sub
I used "If Now > time1.AddSeconds(9)"
instead of "If Now = time1.AddSeconds(10)"
Just in case something else interferred with the loop at the precise count of 10 seconds - very unlikely but possible. This code would not work if the 10 second delay period started within the 9 seconds prior to the ending of Daylight Saving Time, when it would result in a 1 hour 10 second delay.
Last edited by taxes; May 24th, 2004 at 05:34 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 24th, 2004, 08:13 AM
#5
Sleep mode
Re: loops
Originally posted by System_Error
I know how to make something loop for a fixed number of times(10 for example) but, I would like to know how to make something loop for a minute or a few seconds. I know you would probably use the Timer control but im not sure how. Can someone help me?
according to his question , I showed him a very basic way and he can figure out the reset of the code .
When you put some code in Tick event of a timer and set interval to let's 3000 (3sec) . This means , it use that code for 3 sec then waits for another 3sec and then resume and so on .
-
May 24th, 2004, 06:22 PM
#6
PowerPoster
Hi Pirate,
"When you put some code in Tick event of a timer and set interval to let's 3000 (3sec) . This means , it use that code for 3 sec then waits for another 3sec and then resume and so on ."
Where did you get that idea from??? I suggest you try it out. You will find it behaves as I posted. The tick event will fire every 3 seconds, but the loop contained in the tick event will complete in less than a millisecond. Try it yourself and see.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 24th, 2004, 06:36 PM
#7
Sleep mode
lol
Dude , I know that . I was not referring to the code I posted . I was talking about the Tick event in general . And , I know that my code wasn't really working as he wants it but at least he should do that by himself .
-
May 24th, 2004, 06:50 PM
#8
PowerPoster
Originally posted by Pirate
lol
Dude , I know that . I was not referring to the code I posted . I was talking about the Tick event in general . And , I know that my code wasn't really working as he wants it but at least he should do that by himself .
"When you put some code in Tick event of a timer and set interval to let's 3000 (3sec) . This means , it use that code for 3 sec then waits for another 3sec and then resume and so on ."
Please carefully re-read your posts. The Tick event will fire once every 3 seconds but it will use the code it contains only once, and that, in your posting, will be less than a millisecond. Under no circumstances would the code you first posted cause a 10 second delay.
Hi System_error,
Hope you are not getting confused by this.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 24th, 2004, 07:04 PM
#9
Sleep mode
Are you trying to teach me ?? you're not supposed to do that . I know what the hell I'm doing . I told you , forget about the code I posted . I was explaining how Tick event works . That's all .
-
May 25th, 2004, 04:49 AM
#10
Member
One way of doing it is defining a time global variable. And the use of dovents Eg. Below.
VB Code:
Dim j As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Interval = 1000
Timer1.Enabled = True
For i As Integer = 1 To 1000000
Me.Text = i & ": " & j
System.Windows.Forms.Application.DoEvents()
If j = 5 Then
Timer1.Enabled = False
Exit For
End If
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
j += 1
But there's a performance lost.
It is recommenc to use a gettickcount or something similar.
-
May 25th, 2004, 05:27 AM
#11
Member
Second method
VB Code:
Dim startTime As Integer = System.Environment.TickCount.ToString
Dim stopTime As Integer
For i As Integer = 1 To 1000000
Me.Text = i & ": " & j
stopTime = System.Environment.TickCount.ToString
If stopTime - startTime >= 5000 Then 'run 5 second then stop
Exit For
End If
Next
The third method would be using time span
-
May 25th, 2004, 06:01 AM
#12
PowerPoster
Originally posted by Pirate
Are you trying to teach me ?? you're not supposed to do that . I know what the hell I'm doing . I told you , forget about the code I posted . I was explaining how Tick event works . That's all .
Pirate, the tick event does NOT work how you said. If you are not prepared to check out what you are posting, I give up. I sugest we abandon this futile correspondence.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 25th, 2004, 06:34 AM
#13
PowerPoster
Hi vutle,
The danger with using DoEvents in your first method is that the user COULD press another button which might interfere with the loop.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 25th, 2004, 09:09 AM
#14
Member
-
May 25th, 2004, 10:30 AM
#15
PowerPoster
Hi vutle,
"Yes... That's the reason of using a doevents. It could be use to stop or cancel the loop without using thread. "
Understood. I suppose it depends on why the delay is required.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|