-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
  • So what is the value of `dataprogbar.Value` when you are 'done searching'? I'm guessing it's not 100. – The Blue Dog May 12 '15 at 21:04
  • @TheBlueDog the maximum value is set to 100 – jake pavek May 12 '15 at 21:15
  • @TheBlueDog I added the whole code so the maximum = e.progresspercentage – jake pavek May 12 '15 at 21:17
  • The first comment is probably key - you are not getting to the point where .Value = 100. I'm not sure this is a good way to indicate you are done searching. What do you think CBool(e.ProgessPercentage) is doing? Doesn't make much sense - are you testing for <> 0 – rheitzman May 12 '15 at 21:25
  • 1
    I'll ask again, what is the value of `dataprogbar.Value` when you are 'done searching'? Whatever you have set the maximum value to is irrelevant at this point. – The Blue Dog May 12 '15 at 21:26
  • @TheBlueDog the value should be 100 when done searching, I updated all the backgroundworker code. thanks. Regards Jake. – jake pavek May 12 '15 at 21:40
  • 1
    "`the value should be 100 when done searching`" It ***should be***, or ***it is*** = 100? – alstonp May 12 '15 at 21:55
  • @alstonp I don't know what you mean :( I updated the code. It says the value = e.progressprecentage. I am really trying here but I just can't comprehend what you are saying. – jake pavek May 12 '15 at 22:04
  • Basically, we're asking did you actually observe the value being 100 at the end of the search? – alstonp May 12 '15 at 22:06
  • Oh okay, yes. It finishes the search with the last text document filepath in the label. @alstonp – jake pavek May 12 '15 at 22:08
  • Got ya, I'm writing a sample app now – alstonp May 12 '15 at 22:09
  • The progress bar goes all the way up before it's done, maybe it's switch the name but then switching back to the filepath – jake pavek May 12 '15 at 22:13
  • There is the problem then, could you help me with a remedy? This is the only way I know of doing it. @alstonp – jake pavek May 12 '15 at 22:16
  • Sorry I was wrong about that, I just realized... – alstonp May 12 '15 at 22:17

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
  • 700
  • 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