Results 1 to 7 of 7

Thread: [RESOLVED] BGW TextFieldParser ReportProgress

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Resolved [RESOLVED] BGW TextFieldParser ReportProgress

    Hi guys, I am trying to implement a BGW and want to report progress to a progress bar for my TextFieldParser program, sometimes if your opening very large files it takes a while and I would like the user to be able to see the progress and have some interaction so they know the program is still running

    I am a little hung up on what to in the reportprogress

    I have:

    worker.ReportProgress(0, myReader.ReadFields())

    and its not doing anything, I didnt expect it too since ReadFields is an array

    any suggestions or help would be appreciated here is the code for my TextFieldParser

    parser Code:
    1. Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    2.         Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
    3.  
    4.         Dim safeFileName As String = Me.OpenFileDialog1.FileName
    5.         Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(safeFileName)
    6.             myReader.SetDelimiters(vbTab)
    7.             Dim currentRow As String()
    8.             currentRow = myReader.ReadFields()
    9.             Dim colNameList As New List(Of String)
    10.             Dim colName As String = String.Empty
    11.  
    12.             For i As Integer = 0 To currentRow.GetUpperBound(0)
    13.  
    14.                 colName = currentRow(i)
    15.                 Dim suffix As Integer = 1
    16.                 While colNameList.Contains(colName)
    17.                     colName = currentRow(i) & suffix.ToString
    18.                     suffix += 1
    19.                 End While
    20.                 colNameList.Add(colName)
    21.  
    22.             Next
    23.             For Each currentField As String In colNameList
    24.                 table.Columns.Add(currentField, GetType(System.String))
    25.  
    26.             Next
    27.             While Not myReader.EndOfData
    28.                 Try
    29.                     currentRow = myReader.ReadFields()
    30.                     table.Rows.Add(currentRow)
    31.                     worker.ReportProgress(0, myReader.ReadFields())
    32.                 Catch ex As Exception
    33.                 End Try
    34.             End While
    35.         End Using
    36.         If table.Columns.Contains("Column1") Then
    37.             table.Columns.Remove("Column1")
    38.         End If
    39.         dtAll = table
    40.     End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: BGW TextFieldParser ReportProgress

    Um, if you want to report progress then shouldn't you be passing something that indicates progress? How is the number zero and the data you just read indicating progress? Progress would normally be a number, either absolute or percentage. If you want to report the number of records read then you have to count the number of records read and report it. If you want to report a percentage then you'll need to know the number read and the total number. Do you know the total number? Presumably not, so you can't do that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: BGW TextFieldParser ReportProgress

    Yes I knwo thats why I posted the question. I am stuck. I didnt expect what I had to work

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: BGW TextFieldParser ReportProgress

    If you can't measure the progress then you can't report the progress. If you don't know how many records there are then you can't report what proportion of the total you have read. If you want to know how many records there are then you would have to read in the file and split the lines to do so. One option might be to create the TextFieldParser like this:
    vb.net Code:
    1. Dim lines = IO.File.ReadAllLines("file path here")
    2. Dim recordCount = lines.Length
    3. Dim originalText = String.Join(Environment.NewLine, lines)
    4. Dim recordNumber = 0
    5.  
    6. Using reader As New IO.StringReader(originalText),
    7.       parser As New FileIO.TextFieldParser(reader)
    8.     Do Until parser.EndOfData
    9.         Dim fields = parser.ReadFields()
    10.  
    11.         '...
    12.  
    13.         recordNumber += 1
    14.  
    15.         Dim progress = CInt(100 * recordNumber / recordCount)
    16.     Loop
    17. End Using
    That does mean that the actual reading of the file will be outside the progress calculation though, and only the processing of the data will be reported on.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: BGW TextFieldParser ReportProgress

    Thank you
    For now I just have the count being reported as they are read inot a label, this provides some interaction and lets the user know the program is still running. I personally like the look of a progress bar so i will look inot implementing what you suggested

    Still learning and wanted to make sure I wasnt missing something, but from what I can tell there is no way to know the total lines/records using parser the way I currently am, so hence no way to measure progress as a percentage obviously

    Thanks for your help

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: BGW TextFieldParser ReportProgress

    It's not really anything to do with the TextFieldParser specifically. It's the simple fact that you can't know how many lines are in a file without reading those lines. A different approach might be to use the number of Bytes rather than the number or records. Something like this:
    vb.net Code:
    1. Using reader As New IO.StreamReader("file path here"),
    2.       parser As New FileIO.TextFieldParser(reader)
    3.     Dim baseStream = reader.BaseStream
    4.     Dim totalBytes = baseStream.Length
    5.  
    6.     Do Until parser.EndOfData
    7.         Dim fields = parser.ReadFields()
    8.  
    9.         '...
    10.  
    11.         Dim progress = CInt(100 * baseStream.Position / totalBytes)
    12.     Loop
    13. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: BGW TextFieldParser ReportProgress

    Thanks,
    I went with number of records works great

    Appreciate it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width