|
-
Jan 19th, 2009, 03:31 PM
#1
Thread Starter
Member
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
-
Jan 19th, 2009, 03:36 PM
#2
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
-
Jan 19th, 2009, 04:19 PM
#3
Thread Starter
Member
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
-
Jan 19th, 2009, 04:33 PM
#4
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?
-
Jan 19th, 2009, 04:38 PM
#5
Lively Member
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)
-
Jan 19th, 2009, 04:51 PM
#6
Thread Starter
Member
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
-
Jan 19th, 2009, 05:02 PM
#7
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
-
Jan 19th, 2009, 09:48 PM
#8
Re: VB6 i want to use a timer to pause a program
 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?? 
-
Jan 19th, 2009, 10:44 PM
#9
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
-
Jan 20th, 2009, 12:06 PM
#10
Thread Starter
Member
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
-
Jan 20th, 2009, 08:24 PM
#11
Re: VB6 i want to use a timer to pause a program
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|