dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: How to create two 30 secs delay in 1000 loops routine

  1. #16
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: How to create two 30 secs delay in 1000 loops routine

    The API GetTickCount() sounds like a good candidate to do this. There is still a case where the value will roll back to 0, but it seems appropriate for what you are doing.

    Also, be careful with construction like this:

    Code:
    Do While Timer <> Start
    This could lead to an infinite loop, the chances are very small, but I've already seen problem with that kind of validation.

    Hope it helps.

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  2. #17
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,321

    Re: How to create two 30 secs delay in 1000 loops routine

    I think a timer is the best option, a loop will use a lot of CPU and is not needed.

    Create a var at the form level and add a timer control to the form

    Set the timer interval to 30,000
    In the button click add code that sets the var to 0 and enables the timer

    In the timer tick event increment the var by 1 and then check to see if the value > 2000. If it is then disable the timer and exit the sub

    If not then use the mod function to see if the value is even.

    If even run routine 1
    Else
    Run routine 2

    the benifit is that there is no loop running and the CPU will be at 0&#37; usage most of the time where as witht he loop method it will run at 100% even though most of the time it is not doing anythign but looping.
    Always use [code][/code] tags when posting code.

  3. #18
    Join Date
    Mar 2011
    Posts
    46

    Re: How to create two 30 secs delay in 1000 loops routine

    Quote Originally Posted by JeffB View Post
    The API GetTickCount() sounds like a good candidate to do this. There is still a case where the value will roll back to 0, but it seems appropriate for what you are doing.

    Also, be careful with construction like this:

    Code:
    Do While Timer <> Start
    This could lead to an infinite loop, the chances are very small, but I've already seen problem with that kind of validation.

    Hope it helps.

    JeffB
    Sigh .. GetTickCount IS WHAT VB timer call is it's just corrected to midnight

    And it it is impossible to infinite loop off such a call the modulo restricts the range to valid if you don't understand that then you really shouldnt be offering advice.

  4. #19
    Join Date
    Oct 2010
    Posts
    21

    Thumbs up Re: How to create two 30 secs delay in 1000 loops routine

    Quote Originally Posted by DataMiser View Post
    I think a timer is the best option, a loop will use a lot of CPU and is not needed.

    Create a var at the form level and add a timer control to the form

    Set the timer interval to 30,000
    In the button click add code that sets the var to 0 and enables the timer

    In the timer tick event increment the var by 1 and then check to see if the value > 2000. If it is then disable the timer and exit the sub

    If not then use the mod function to see if the value is even.

    If even run routine 1
    Else
    Run routine 2

    the benifit is that there is no loop running and the CPU will be at 0% usage most of the time where as witht he loop method it will run at 100% even though most of the time it is not doing anythign but looping.

    Hi DataMiser,
    Thanks for the suggestion, from my understanding this will run when the form is load. How about this code to use as command button event? Can you show an example? Sorry I'm still in entry level.
    Thanks.

  5. #20
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: How to create two 30 secs delay in 1000 loops routine

    Quote Originally Posted by Uglybb View Post
    Sigh .. GetTickCount IS WHAT VB timer call is it's just corrected to midnight

    And it it is impossible to infinite loop off such a call the modulo restricts the range to valid if you don't understand that then you really shouldnt be offering advice.
    Sigh ... Some people ......

    'GetTickCount IS WHAT VB timer call' - Hmm actually it is not ... GetTickcount (MSDN Link), gets the number of ticks from startup from the bios clock, and can rollover to 0 when the bits run out (49.7 days), not when the clock reaches midnight..

    VB Timer controls do not CALL gettickcount, but is attached to a hardware timer, and is accurate to 16 ms......

    Then .. JeffB is a old time poster here, and is actually a forum Moderator, He got that status by knowing what he's talking about, and giving Good to Excellent advice.. I'd listen to him....

    Also DoEvents is one of the most DANGEROUS items to put inside a loop.. Read this MSDN Blog for more info...

    Use of Doevents = BAD CODE .....

    @UglyBB, Your advice was well aimed but slightly misguided, and it's obvious that you have very little knowledge on the architecture of Windows and VB6, so i'd suggest you bow out of this one...

    Quote Originally Posted by ektan View Post
    Thanks for the suggestion, from my understanding this will run when the form is load. How about this code to use as command button event? Can you show an example? Sorry I'm still in entry level.
    @ektan, Look at the Timers Control article for some sample code on how to properly use timers to complete such tasks.. I'd also recomend the StickFigure animation project from the Animation in VB article on how to use a command button to start it.....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #21
    Join Date
    Oct 2010
    Posts
    21

    Re: How to create two 30 secs delay in 1000 loops routine

    Hi GremlinSA,
    Thanks for the advice and guidence on understanding the timer function.

    Hi All,
    Thanks for the guidence too.

    Thanks you very much.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)