Solved

Do Until with a timer issues...

Posted on 2008-09-30
2
182 Views
Last Modified: 2010-04-21
I am working on some code that contains a Do Until statement that continually pings a computer until it comes online or the timer reaches 5 seconds. The reason I want it to wait and keep trying is that a previous script attempted to power on the computer. The problem I'm running into, is that the timer doesn't seem to do anything unless I put a msgbox in the loop statement. Only then does the timer do anything with the counter variable. I'd much rather I didn't have to continually click OK until it's done. Ideas?
Private Sub BackgroundWorker4_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker4.DoWork
 
            ifonline1 = 0
 
            counter = 0
 
            InitializeTimer()
            MsgBox("Attempting to power on " & strcomputer)
            Do Until ifonline1 = 1 Or counter >= 5
 
                On Error Resume Next
                Dim Ping As New Net.NetworkInformation.Ping
                Dim PingOptions As New Net.NetworkInformation.PingOptions
                PingOptions.Ttl = 64
                Dim PingReply As Net.NetworkInformation.PingReply = Ping.Send(strcomputer, 200)
                If PingReply.Status = Net.NetworkInformation.IPStatus.Success Then
                    ifonline1 = 1
                    flag4 = "ON"
                Else
                    ifonline1 = 0
                    flag4 = "OFF"
                End If
 
            Loop
            MsgBox(flag4)
            If ifonline1 = 1 Then
                Dim Host As Net.IPHostEntry = Net.Dns.GetHostEntry(strcomputer)
                Dim firstaddress As Net.IPAddress = Host.AddressList(0)
                ip = firstaddress.ToString
                
            End If
            flag1 = pc3
            flag2 = ip
            flag3 = rownum
            worker.ReportProgress(prog1)
        Next
 
End Sub
 
Private Sub InitializeTimer()
        ' Run this procedure in an appropriate event.
        counter = 0
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
 
        If counter >= 6 Then
            ' Exit loop code.
            Timer1.Enabled = False
            counter = 0
        Else
            'MsgBox(counter)
            ' Run your procedure here.
            ' Increment counter.
            counter = counter + 1
        End If
 
    End Sub

Open in new window

0
Comment
Question by:jcamping
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 95 total points
ID: 22610619
I would just get rid of the Timer altogether and add the timing logic directly into your loop.

Here is the big change:

            Dim endTime As DateTime = DateTime.Now.AddSeconds(5)
            Do Until ifonline1 = 1 OrElse DateTime.Now.Subtract(endTime).TotalMilliseconds > 0

See full code below:
        Private Sub BackgroundWorker4_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker4.DoWork
            ifonline1 = 0
            MsgBox("Attempting to power on " & strcomputer)
 
            Dim endTime As DateTime = DateTime.Now.AddSeconds(5)
            Do Until ifonline1 = 1 Or DateTime.Now.Subtract(endTime).TotalMilliseconds > 0
 
                On Error Resume Next
                Dim Ping As New Net.NetworkInformation.Ping
                Dim PingOptions As New Net.NetworkInformation.PingOptions
                PingOptions.Ttl = 64
                Dim PingReply As Net.NetworkInformation.PingReply = Ping.Send(strcomputer, 200)
                If PingReply.Status = Net.NetworkInformation.IPStatus.Success Then
                    ifonline1 = 1
                    flag4 = "ON"
                Else
                    ifonline1 = 0
                    flag4 = "OFF"
                End If
            Loop
            MsgBox(flag4)
            If ifonline1 = 1 Then
                Dim Host As Net.IPHostEntry = Net.Dns.GetHostEntry(strcomputer)
                Dim firstaddress As Net.IPAddress = Host.AddressList(0)
                ip = firstaddress.ToString                
            End If
            flag1 = pc3
            flag2 = ip
            flag3 = rownum
            worker.ReportProgress(prog1)
        Next
     End Sub

Open in new window

0
 

Author Closing Comment

by:jcamping
ID: 31501791
That works great! Thanks!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now