Results 1 to 15 of 15

Thread: loops

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Place a label and a timer and set its interval to 1000 and enabled property to True .
    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e
    2. As System.EventArgs) Handles Timer1.Tick
    3.  
    4.         For i As Integer = 0 To 10
    5.             Me.Label1.Text += i.ToString
    6.         Next
    7.  
    8. End Sub

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Pirate
    Place a label and a timer and set its interval to 1000 and enabled property to True .
    VB Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e
    2. As System.EventArgs) Handles Timer1.Tick
    3.  
    4.         For i As Integer = 0 To 10
    5.             Me.Label1.Text += i.ToString
    6.         Next
    7.  
    8. 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:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         I += 1
    3.         If I = 10 Then
    4.             Timer1.Enabled = False
    5.             I = 0
    6.         End If
    7.     End Sub

    P T Exorcist's suggestion also works OK but to clarify it use the following code:

    VB Code:
    1. Private Sub Delay10()
    2.         Dim time1 As Date = Now
    3.         Do
    4.             If Now > time1.AddSeconds(9) Then Exit Do
    5.         Loop
    6. 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.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    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 .

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  10. #10
    Member
    Join Date
    Aug 2003
    Posts
    51
    One way of doing it is defining a time global variable. And the use of dovents Eg. Below.

    VB Code:
    1. Dim j As Integer
    2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.     Timer1.Interval = 1000        
    4.     Timer1.Enabled = True
    5.  
    6.  
    7.         For i As Integer = 1 To 1000000
    8.             Me.Text = i & ": " & j
    9.             System.Windows.Forms.Application.DoEvents()
    10.             If j = 5 Then
    11.                 Timer1.Enabled = False
    12.                 Exit For
    13.             End If
    14.         Next
    15.     End Sub
    16.  
    17.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    18.         j += 1
    But there's a performance lost.
    It is recommenc to use a gettickcount or something similar.

  11. #11
    Member
    Join Date
    Aug 2003
    Posts
    51
    Second method

    VB Code:
    1. Dim startTime As Integer = System.Environment.TickCount.ToString
    2.         Dim stopTime As Integer
    3.  
    4.         For i As Integer = 1 To 1000000
    5.             Me.Text = i & ": " & j
    6.             stopTime = System.Environment.TickCount.ToString
    7.             If stopTime - startTime >= 5000 Then  'run 5 second then stop
    8.                 Exit For
    9.             End If
    10.         Next

    The third method would be using time span

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  14. #14
    Member
    Join Date
    Aug 2003
    Posts
    51
    Originally posted by taxes
    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.
    Yes... That's the reason of using a doevents. It could be use to stop or cancel the loop without using thread.

    I just want to show that, you can apply many methods to that question.

    He or she can also, get the current time stamp, then within the time get the timestamp differences. This would be the fourth method on the question.

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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
  •  



Click Here to Expand Forum to Full Width