|
-
Feb 14th, 2012, 04:02 PM
#1
Thread Starter
Frenzied Member
[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:
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
Dim safeFileName As String = Me.OpenFileDialog1.FileName
Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(safeFileName)
myReader.SetDelimiters(vbTab)
Dim currentRow As String()
currentRow = myReader.ReadFields()
Dim colNameList As New List(Of String)
Dim colName As String = String.Empty
For i As Integer = 0 To currentRow.GetUpperBound(0)
colName = currentRow(i)
Dim suffix As Integer = 1
While colNameList.Contains(colName)
colName = currentRow(i) & suffix.ToString
suffix += 1
End While
colNameList.Add(colName)
Next
For Each currentField As String In colNameList
table.Columns.Add(currentField, GetType(System.String))
Next
While Not myReader.EndOfData
Try
currentRow = myReader.ReadFields()
table.Rows.Add(currentRow)
worker.ReportProgress(0, myReader.ReadFields())
Catch ex As Exception
End Try
End While
End Using
If table.Columns.Contains("Column1") Then
table.Columns.Remove("Column1")
End If
dtAll = table
End Sub
-
Feb 14th, 2012, 07:20 PM
#2
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.
-
Feb 14th, 2012, 08:45 PM
#3
Thread Starter
Frenzied Member
Re: BGW TextFieldParser ReportProgress
Yes I knwo thats why I posted the question. I am stuck. I didnt expect what I had to work
-
Feb 14th, 2012, 09:19 PM
#4
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:
Dim lines = IO.File.ReadAllLines("file path here") Dim recordCount = lines.Length Dim originalText = String.Join(Environment.NewLine, lines) Dim recordNumber = 0 Using reader As New IO.StringReader(originalText), parser As New FileIO.TextFieldParser(reader) Do Until parser.EndOfData Dim fields = parser.ReadFields() '... recordNumber += 1 Dim progress = CInt(100 * recordNumber / recordCount) Loop 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.
-
Feb 14th, 2012, 09:49 PM
#5
Thread Starter
Frenzied Member
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
-
Feb 14th, 2012, 09:57 PM
#6
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:
Using reader As New IO.StreamReader("file path here"), parser As New FileIO.TextFieldParser(reader) Dim baseStream = reader.BaseStream Dim totalBytes = baseStream.Length Do Until parser.EndOfData Dim fields = parser.ReadFields() '... Dim progress = CInt(100 * baseStream.Position / totalBytes) Loop End Using
-
Feb 15th, 2012, 09:58 PM
#7
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|