CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Oct 2010
    Posts
    21

    How to create two 30 secs delay in 1000 loops routine

    Hi all,
    I was stuck on writting this code for the whole day and night which still not working as I was using the VB6 timer. The structure of the code will execute once the command buttom was clicked. Like example below:

    When command buttom was clicked:
    For i = 1 to 1000
    mycode01
    delay 30 seconds
    mycode02
    delay 30 seconds
    next i

    I had been using the system timer but when it reach to midnight then my code just gone crazy.
    Please help me to out of the circle.
    Thanks.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

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

    What do you want to do? What is code01 and code02? Perhaps if we know what you are trying to achieve, we can perhaps suggest proper solutions or alternatives...

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

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

    There is the sleep routine, provide by the API
    Code:
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    ...
    Sleep 30000
    ...
    The Sleep instructions suspends code execution for a given number of milliseconds.
    Disadvantage is that your program would not respond anymore for the given time.

    Another solution would be to use a timer control. You could set the time to 30000 msecs
    and enable the timer. In the Timer_Timer() event you will disable the timer.
    In your code you need a line to wait for the disabled timer.
    Assume Timer1 added to your form
    Code:
    Private Sub Timer1_Timer()
      Timer1.Enabled = False
    End Sub
    
    'in your code loop
    ...
    While Timer1.Enabled: DoEvents: Wend 'wait for the timer to expire
    Thuis will leave your program responding to otehr mouseclicks and refreshes properly.

  4. #4
    Join Date
    Oct 2010
    Posts
    21

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

    Thanks to Hannesthegreat and Wof reply my post. First I would like to say sorry for unclear information of my post. Actually I would to make the code to send two command out from serial port for 1000 times and 30 seconds interval for each command to be execute. This code will place into the command button. Once the buttom click, the code will execute to 1000 times.
    Example:

    Private Sub cmdLoops_Click()
    'open the com port
    '1000 loops start
    'send first command to com port
    'delay 30 seconds
    'send second command to com port
    'delay 30 seconds
    'Once reach 1000 loops
    'stop send command
    Exit Sub

    That's it.
    Thanks.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

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

    Need to see more code than that. Describe what you're sending 1000 times, and we'll know more

    Code:
    ' Use CODE TAGS also
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Oct 2010
    Posts
    21

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

    Hi dglienna,
    Below is my code that seems to be work but when it reach to midnight my code will go crazy, this is what I want to escape from this problem and try to use the timer as a delay for my code. Unfortunately, I can't make it work as what I expected.

    Private Sub cmd1000LPC03_Click()
    Dim Start As Long
    sbrStatus.Panels("ComPort").Text = "Loop: " & PLoop

    For PLoop = 1 To 1000
    Open "c:\mylog.log" For Append As #1
    Print #1, Date & " " & Time & " " & "Loop: " & PLoop
    Print #1, Date & " " & Time & " " & "Power ON..."
    Close #1
    sbrStatus.Panels("ComPort").Text = "Loop: " & PLoop
    MSComm1.Output = "REL1.ON" & vbCrLf 'Turn the relay1 ON
    Start = Timer
    Do While Timer < Start + 30
    DoEvents ' Yield to other processes.
    Loop

    Open "c:\mylog.log" For Append As #1
    Print #1, Date & " " & Time & " " & "Power OFF..."
    Close #1
    MSComm1.Output = "REL1.OFF" & vbCrLf
    Start = Timer
    Do While Timer < Start + 30
    DoEvents ' Yield to other processes.
    Loop
    Beep
    Next
    MsgBox "1000 Loops Power Cycle is Complete!", vbOKOnly, "1000 Loops"

    End Sub

    Thanks.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

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

    Sleep is the clear choice but there is another way you could do this.

    Use a timer control and set the interval to 30000.

    Create a counter var that will be incremented by one in the timer tick event.

    In the tick event check to see if the counter is even or odd and run one routine or the other based on the even/odd value of the var.

    When the var reaches 2000 disable the timer

    In the command button click set the counter var to 0 and enable the timer
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

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

    Code:
    Start = Timer ' This negates the next statement
    Do While Timer < Start + 30 ' NEVER less unless code takes 15ms
    DoEvents ' Yield to other processes. ' Doesn't do much
    Loop
    I agree with the above. Just use code to calculate things.

    I don't know why you have 1000 iterations...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Oct 2010
    Posts
    21

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

    Hi DataMiser,
    Thanks for the replay, can elebrate more on your explanation. Sorry about that, because I'm still in entry level.

    Hi dglienna,
    Yes, the reason 1000 iterations is to ensure the device able to come online every power on/off cycle.

    Thanks.

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

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

    What power-cycles that many times?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Oct 2010
    Posts
    21

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

    Hi dglienna,
    I wrote this code is for my project that the code will communicate with device & power switch through RS-232 com port. This is some kind like device power cycle reliability testing to ensure the device able to completely came online every power cycle.
    Thanks.

  12. #12
    Join Date
    Mar 2011
    Posts
    46

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

    If you dont want to change your code it is easy to fix

    All that is happening is the timer count is rolling it is a lonint of the seconds count from midnight

    (Timer) CurrentSecondOfDay = (Hour24 * 3600) + (Minute * 60) + Second

    And as such it goes from 0-86399

    So simply change the test instead of

    Start = Timer
    Do While Timer < Start + 30
    DoEvents ' Yield to other processes.
    Loop

    Use

    Start = (Timer + 30) Mod 86400
    Do While Timer < Start
    DoEvents ' Yield to other processes.
    Loop

    All that is happening is we are turning start to a compare value after first adding 30 and modulo'ing it.

    If you consider we call it 1 sec before midnight timer will return 86399

    So

    start = (86399 + 30) Mod 86400
    start = 86429 Mod 86400
    start = 29

    Timer will pass the test (Timer < Start) because 86399 and the next second it will roll itself back to zero and the test will continue correctly until Timer is 29

    Modulo is a very powerful tool with rolling counts you need to get familar with it.
    if (Diff > 100) Diff

  13. #13
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

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

    BB, please use CODE TAGS.

    LONG's are the wrong way to go. Use the TIMER to calculate ONE-SECOND, and then USE THE TIME!
    Code:
    Option Explicit
    ' Add a Timer control to your project.  It will be Timer1
    ' It looks like a stop watch in the IDE.
    Dim OldTime As Date
    Dim newTime As Date
    Dim diff As Date
    
    Private Sub Form_Load()
      OldTime = Time
      Timer1.Interval = 1000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Static x As Long
      Static zz$, ss$
      x = x + 1
      newTime = Time
      diff = DateDiff("s", OldTime, newTime)
      Form1.Caption = (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
            Format((diff - ((diff \ 60) * 60)), "00")
    '  Debug.Print (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
            Format((diff - ((diff \ 60) * 60)), "00")
      If newTime = DateAdd("s", 360, OldTime) Then ' Add 360 seconds
        Timer1.Enabled = False
        ' You time is UP!  Do something!
        Beep
      End If
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Mar 2011
    Posts
    46

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

    Your changes are fine but he has to heavily modify the code from what I can see he has an old command line program.

    Timer is already the system time as a long I am not somehow making a long ... VB is simply going down to the RTC and getting the bytes from it doing the mult and giving it back to you as a long ... it's what the call Timer does!!!!

    All I am doing is put pre adding and putting a modulo on the result he has to change 4 lines to fix the code.

    old code:
    Start = Timer
    Do While Timer < Start + 30
    DoEvents ' Yield to other processes.
    Loop

    becomes in two places and his problem solved

    new code:
    Start = (Timer+30) Mod 86400
    Do While Timer < Start
    DoEvents ' Yield to other processes.
    Loop

  15. #15
    Join Date
    Mar 2011
    Posts
    46

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

    Urg too much coffee today .. changed new code to

    new code:
    Start = (Timer+30) Mod 86400
    Do While Timer <> Start
    DoEvents ' Yield to other processes.
    Loop

Page 1 of 2 12 LastLast

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

Featured