stop the loop?

skyfe

Member
Joined
Jan 16, 2009
Messages
14
Programming Experience
3-5
Hi,

I'v got 2 subs that have a for loop that keeps getting/searching for files/directories and returning the path of the files. These subs get active when someone hits the "Start Scan" button, but now I also want to make a "Stop Scan" button to stop the loop. But how can I do this? Because when I let it create a basic "Button_Click" sub, then it doesn't work because it keeps scanning files (because it stays in the for loop where it searches for files and returns the paths, so it doesn't continue to the next sub where it checks if the button "Stop Scan" has been pressed, but I want it to check if that button has been pressed, like constantly while it scans, how can I do this?

Thanks in advanced again, really appreciated,

Skyfe.

btw, this is a part of the code I got now:

VB.NET:
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

        Dim CurrentScanStatus As Object = DoScanStatus("")

        MsgBox(CurrentScanStatus)

        If CurrentScanStatus = "Scan has not started yet." Then
            DoScanStatus("Scan Started")

            CurrentScanStatus = DoScanStatus("")
            Label1.Text = CurrentScanStatus & "                                 "
            Label1.Update()
            Sleep(500)

            Button9.Enabled = False
            Button8.Enabled = True

        End If

        'Search C:\ top level subdirectories

        Dim foundDirs2 = My.Computer.FileSystem.GetDirectories("C:\", FileIO.SearchOption.SearchTopLevelOnly, "*")
        Dim AmountFoundDirs2 As Integer

        AmountFoundDirs2 = foundDirs2.Count
        ProgressBar2.Maximum = AmountFoundDirs2

        If ScanStatus = "Scan Started" Then

            For Each foundDir As String In My.Computer.FileSystem.GetDirectories _
            ("C:\", FileIO.SearchOption.SearchTopLevelOnly, "*")

                If CurrentScanStatus = "Scan Started" Then


                    AddDirectoryLog(foundDir)
                    ProgressBar2.Value = ProgressBar2.Value + 1
                    Sleep(100)


                End If

            Next

        End If

    End Sub


    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

        ScanStatus = "Scan Stopped"
        Button8.Enabled = False
        Button9.Enabled = True
        DoScanStatus(ScanStatus)

    End Sub
 
Last edited by a moderator:
Don't do lenghty work in UI thread. Have a look a how to use BackgroundWorker Class. It has convenient threading features like progress reporting to UI thread, cancellation and busy info.
 
Back
Top