Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

End less loop with STOP!

Status
Not open for further replies.

Shift838

IS-IT--Management
Jan 27, 2003
987
0
0
US
I need to implement an Endless loop for the below code. I want to be able to click a button to stop the execution of the below code. How can i create an endless loop that will continue to do the code until I click a stop button?

Code:
Private Sub cmdStartPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartPing.Click
        Dim serverlist, x As Integer
        Dim servers As String
        Dim response As New ListViewItem

        serverlist = lstServers.Items.Count - 1

        For x = 0 To serverlist

            servers = lstServers.Items(x).Text
            MultiPingSystem(servers)

            If pingresults = "Success" Then
                lstServers.Items(x).BackColor = Color.GreenYellow
                lstServers.Items(x).SubItems.Add("Success")
                lstServers.Refresh()
                lstServers.Items(x).SubItems.Add(roundtriptime)
                lstServers.Refresh()
            Else
                lstServers.Items(x).BackColor = Color.Red
                lstServers.Items(x).SubItems.Add("No Reply")
                lstServers.Refresh()
                lstServers.Items(x).SubItems.Add("Timeout")
                lstServers.Refresh()
                lstServers.Items(x).SubItems.Add(currentdt)
                lstServers.Refresh()

            End If
        Next

    End Sub
 
Put this in the global Declarations area of the form:

Dim bCancel As Boolean = False


The in the Cancel button's Click event handler:

bCancel = True


Then in the loop:

Application.DoEvents()

If bCancel Then
bCancel = False
Exit For
End If

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks jebenson. works great. but now i have an issue with the loop that it is not updating the subitems correctly, just keeps adding "success" or "No Reply" in the wrong columns. What could be my issue?

Code:
Private Sub cmdStartPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartPing.Click
        Dim serverlist, x As Integer
        Dim servers As String
        Dim response As New ListViewItem

        serverlist = lstServers.Items.Count - 1

        For x = 0 To serverlist
            Do While bCancel = False
                Application.DoEvents()

                If bCancel Then
                    bCancel = False
                    MsgBox("ping stopped")
                    Exit For
                End If
                servers = lstServers.Items(x).Text
                MultiPingSystem(servers)

                If pingresults = "Success" Then

                    lstServers.Items(x).BackColor = Color.GreenYellow
                    lstServers.Items(x).SubItems.Add("Success")

                    lstServers.Refresh()
                    lstServers.Items(x).SubItems.Add(roundtriptime)
                    lstServers.Refresh()
                Else
                    lstServers.Items(x).BackColor = Color.Red
                    lstServers.Items(x).SubItems.Add("No Reply")
                    lstServers.Refresh()
                    lstServers.Items(x).SubItems.Add("Timeout")
                    lstServers.Refresh()
                    lstServers.Items(x).SubItems.Add(currentdt)
                    lstServers.Refresh()

                End If
            Loop
        Next

    End Sub
 
it also is not going past the first entry now in the listview control.
 
i changed :

Code:
If bCancel Then
bCancel = True

and it loops good now, but still have the issue with not updating the correct column for the subitem after the first loop.
 
Hi Chris,

Your last post is a redundant change. It should remain the way it was. This is how your code would be evaluated:
Code:
If TRUE then
   TRUE = TRUE
endif

I believe your issue with not updating the correct column is because you acheived your goal. You have an endless loop created by Do While ... Since this loop is endless, the for loop doesn't get incremented. The Do loop is the internal loop so the exit should be Exit Do, not Exit For.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Code:
For x = 0 To serverlist
   Do While bCancel = False
      Application.DoEvents()

      If bCancel Then
         bCancel = False
         MsgBox("ping stopped")
         Exit For
      End If
...
   Loop
Next

should be

Code:
Do While bCancel = False
   For x = 0 To serverlist
      Application.DoEvents()

      If bCancel Then
         bCancel = False
         MsgBox("ping stopped")
         Exit Do
      End If
...
   Next
Loop

that way it will continue to loop through the servers until you click your button
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top