-1

I have this code which works with the filepath but I am trying to get it to change the text to "Search completed" once it is done searching. This is what I tried and I tried it without the quotes around 100 but it still isn't working, any pointers.

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim argument = DirectCast(e.Argument, Tuple(Of String, String))
    Dim target = argument.Item1
    Dim folderPath = argument.Item2
    Dim filePaths = IO.Directory.GetFiles(folderPath, "*.txt")

    'Report the total file count.
    Me.BackgroundWorker1.ReportProgress(filePaths.Length)

    For Each filePath In filePaths
        'Report progress.
        Me.BackgroundWorker1.ReportProgress(CInt(False), filePath)

        If IO.File.ReadAllText(filePath).Contains(target) Then
            'Report a successful search.
            Me.BackgroundWorker1.ReportProgress(CInt(True), filePath)
        End If
    Next

End Sub
Private Sub backgroundworker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Dim filePath = DirectCast(e.UserState, String)

    If filePath Is Nothing Then
        'This is the total file count.
        Me.dataprogbar.Maximum = e.ProgressPercentage
    ElseIf CBool(e.ProgressPercentage) Then
        'This is a successful search.
        Me.ListBox1.Items.Add(filePath)
    Else
        'This is a simple progress update.
        Me.dataprogbar.PerformStep()
        Me.Label1.Text = filePath
        If dataprogbar.Value = "100" Then
            Label1.Text = "Search Completed"
        End If
    End If
End Sub
jake pavek
  • 11
  • 6

1 Answers1

0

There are a few problems here, but the commentators are correct, you are not getting to 100. Additionally, the link here https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 will show you how to use a backgroundworker properly. This similar question here will show you how to use the two together: Running a method in BackGroundWorker and Showing ProgressBar

Community
  • 1
  • 1
alstonp
  • 680
  • 6
  • 25
  • Progressbar maxes out before it's done and still don't change the label :/ – jake pavek May 12 '15 at 22:32
  • Ok so it's saying search completed before it is completing and continues searching even though it says its complete. – jake pavek May 12 '15 at 22:36
  • Now it is adding all the textfiles instead of where the search string is located, and still continues to search when search is completed, I appreciate all you help pal. – jake pavek May 12 '15 at 22:44
  • Np, and your progress bar won't fill up unless you "reportprogress" as many times as you have your maximum value set to. So if you keep adding the count of ***all the files*** and only update ***on a match*** your never going to fill up the progress bar. – alstonp May 12 '15 at 22:50
  • Also, unless someone else swoops in I'll answer anymore questions when I get home. :) – alstonp May 12 '15 at 22:54
  • Noone else will swoop in trust me, I'll wait cause I don't quite get that, as you gathered I am new to this just testing stuff out. – jake pavek May 12 '15 at 22:55
  • let me know when your home @alstonp – jake pavek May 13 '15 at 01:25
  • You will need to tell your `BackgroundWorker1_DoWork` method to stop doing work once your threshold is reached. – alstonp May 13 '15 at 14:05
  • One thing I'd recommend, is understanding this website has a "teach a man to fish" mentality, so this link should provide all the answers you need: https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 – alstonp May 15 '15 at 20:07
  • Yeah that didn't help, appreciate your help. I just won't use it – jake pavek May 18 '15 at 23:38