Results 1 to 11 of 11

Thread: VB6 i want to use a timer to pause a program

  1. #1

    Thread Starter
    Member computerguy46's Avatar
    Join Date
    Jan 2006
    Posts
    32

    Unhappy VB6 i want to use a timer to pause a program

    Hello all
    i am writing a ping program and i want the program to pause for 5 seconds
    before it ping the next ip address. i have set the interval to 5000 and set enabled to false in the properties.

    If Check1.Value = 1 Then
    Timer1.Enabled = True
    Set itmx = ListView1.ListItems.Add(, , Check1.Caption)
    itmx.ForeColor = vbBlack
    Check1.Value = 0
    Text3.Text = Check1.Caption
    Text4.Text = "Booth 44"
    Timer1.Enabled = False
    Call pingchk
    End If

    the above is not working does anyone else ahve anyother id's
    thanks in advance

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB6 i want to use a timer to pause a program

    You could try the SLEEP API instead.

    Code:
    Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    Sample usage:
    Code:
    Debug.Print "The time is "; Time$  ' display the current time
    Sleep 2000  ' 2000 milliseconds = 2 seconds to delay
    Debug.Print "The time is "; Time$  ' this time will be 2 seconds later
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Member computerguy46's Avatar
    Join Date
    Jan 2006
    Posts
    32

    Re: VB6 i want to use a timer to pause a program

    i have tryed sleep but it hangs the program. i was hoping to find a better way
    to do that. thanks

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB6 i want to use a timer to pause a program

    Where are you getting the list for the IP address that you are pinging?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    Lively Member
    Join Date
    Aug 2006
    Posts
    105

    Re: VB6 i want to use a timer to pause a program

    Try using this.

    Sub Pause(Duration)
    starttime = Timer
    Do While Timer - starttime < Duration
    DoEvents
    Loop
    End Sub

    Pause(5)

  6. #6

    Thread Starter
    Member computerguy46's Avatar
    Join Date
    Jan 2006
    Posts
    32

    Re: VB6 i want to use a timer to pause a program

    dee-u
    the ipaddress are hard coded in the program

    JoshHilton
    it did not work but thanks

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB6 i want to use a timer to pause a program

    You can try something like this. You just have to modify it to suit your needs. And set the Timer's interval accordingly.
    Code:
    Option Explicit
    
    Private IP_List(2)  As String
    Private Index       As Long
    
    Private Sub Form_Load()
        IP_List(0) = "x"
        IP_List(1) = "y"
        IP_List(2) = "z"
    End Sub
    
    Private Sub Timer1_Timer()
        If Index <= UBound(IP_List) Then
            ping IP_List(Index)
            Index = Index + 1
        Else
            Index = 0
        End If
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB6 i want to use a timer to pause a program

    Quote Originally Posted by JoshHilton
    Try using this.

    Sub Pause(Duration)
    starttime = Timer
    Do While Timer - starttime < Duration
    DoEvents
    Loop
    End Sub

    Pause(5)
    Your code is a CPU hog. I know because I used to use it. However, the code below uses the same concept but employs the Sleep API with 1mS Sleep calls. This is a very versatile procedure because you can put a Pause call nearly anywhere. It's great for debugging too! Pause 3 = 3 Seconds.

    Code:
    Option Explicit
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    ' Credits: (Milk (Sleep+Pause Sub)). (Wayne Spangler (Pause Sub))
    Private Sub Pause(ByVal Delay As Single)
       Delay = Timer + Delay
       If Delay > 86400 Then              'more than number of seconds in a day
          Delay = Delay - 86400
          Do
              DoEvents                    ' to process events.
              Sleep 1                     ' to not eat cpu
          Loop Until Timer < 1
       End If
       Do
           DoEvents            ' to process events.
           Sleep 1             ' to not eat cpu
       Loop While Delay > Timer
    End Sub
    
    
    Private Sub Form_Load()
       Me.Show
       Dim i As Integer
          For i = 1 To 10
             Me.Caption = "Pausing  " & i
             Pause 1                             ' Call Pause (1 second)
          Next
       Me.Caption = "Done"
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: VB6 i want to use a timer to pause a program

    As a variation on dee-u's approach I like to use something like:
    Code:
    Option Explicit
    
    Private WorkQueue As Collection
    
    Private Sub Form_Load()
        Set WorkQueue = New Collection
        With WorkQueue
            .Add "x"
            .Add "y"
            .Add "z"
        End With
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Dim IP As String
        
        With WorkQueue
            IP = .Item(1)
            ping IP
            .Remove 1
            If .Count < 1 Then Timer1.Enabled = False
        End With
    End Sub
    It retains the advantage of not relying upon the troublesome Sleep() calls and evil DoEvents() calls. It goes further by not relying on arrays requiring purging and resizing during the program's lifetime. For example the program can add new "work requests" at any time simply by:
    Code:
    WorkQueue.Add "new request"
    Timer1.Enabled = True
    For complex workloads you can Add new objects of some Class that contains several value Properties. Sample Class:
    Code:
    Option Explicit
    'WorkLoad.cls
    
    Public ColorResponse As ColorConstants 'Can also use SystemColorConstants.
    Public IP As String
    Public Notation As String
    
    Private Sub Class_Initialize()
        'Set defaults.
        ColorResponse = vbWindowText
    End Sub

  10. #10

    Thread Starter
    Member computerguy46's Avatar
    Join Date
    Jan 2006
    Posts
    32

    Re: VB6 i want to use a timer to pause a program

    thanks for your help i can not see to make this work so i am going back to using sleep

    thanks again

  11. #11
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB6 i want to use a timer to pause a program

    Quote Originally Posted by computerguy46
    thanks for your help i can not see to make this work so i am going back to using sleep

    thanks again
    Then you're doing something wrong. If Sleep works for you then the Pause sub that I posted will also work.... It uses Sleep. Did you run it as is to see how it's called?
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

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