TextinputStream, Progress & Thread

Hallo,

i simple want to load normal Textfiles with different count of lines (From 1 To 1000’s of lines). I would like to show the progress into a Progressbar an a Label. The Label will show the String of the current Line.
[h]Properties[/h]
-Lines() As String
-Progress As Integer
-Text As String
[h]Window-Components[/h]
-Progressbar1
-PushButton1
-Timer1
-Thread1
-Label1
[h]PushButton1 Action Event[/h]

[code]Dim a() As String

Lines = a

Dim f As FolderItem = GetOpenFolderItem(FileTypes1.Gedcom)
If f <> Nil Then
If f.Exists Then
Dim t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8
Lines.Append t.ReadLine
Catch e As IOException
t.Close
End Try

End If
End If

ProgressBar1.Maximum = Lines.Ubound
ProgressBar1.Value = 0
Timer1.Period = 500
Timer1.Mode = timer.ModeMultiple
Thread1.Run
MsgBox(“Done Processing!”)[/code]
[h]Thread1 Run Event[/h]

For i As Integer = 0 To Lines.Ubound Dim p As New LineParser p.Parse(Lines(i)) Progress = i Text = p.Value Next
[h]Timer1 Action Event[/h]

Label1.Text = Text

There will be no run-time error, but neither progress nor the current row is displayed to me.

Take a look in examples/Desktop/UpdatingUIFromThread and either the UIThreadingWithTask or UIThreadingWithTimer examples. Personally, I prefer the Task method.

Thanks Bob! After I did a bit of juggling with the source code, I have found a solution.

After loading the lines into an array, i parse every line. How to get the count of Lines (for the Progressbar Maximum Definition), before reading every Line of the TextinputStream? Because, i do not need the Lines-Array, i can parse the String directly via my LinesParser.Parse(t.ReadLine)…

Ok, i had a look to the “UIThreadingWithTask”-Sample in Xojos “examples/Desktop/UpdatingUIFromThread”.

I’ll load a Textfile (different count of lines, up to > 200.000) with ReadAll and split into an Array “Lines” by EndOfLine. The Maximum of UIProgess is “Lines.bound”. Now i have to process every single Line and I would love to update the Progressbars Value only every 100 Steps or so. The Lines should be add into a Listbox after Processing. But if the Textfile > 10000 Lines, the Window freezes…

Can you follow me? How to do that?

Thank you all!

Doing READLINE has a huge amount of extra overhead as compared to using READALL and SPLIT

READALL requires you have about twice as much as much free memory as the file size to hold at least 2 copies
The first the data you read in to a string or text
The second the data you split it into
Readline has no such overhead

You can read files line by line that exceed 4 Gb but you cant use READALL on them :slight_smile:

Use them accordingly

  1. Try to read a couple of MBs instead of both ReadAll and ReadLine. Requires a bit finessing with reading until the next EndOfLine char.

  2. Use the amount of MBs read/total MBs as value for your progress bar. Check that you don’t update more than once a second.

Maybe not for this case (where you want a smooth running progressbar) but I currently use CallLater to call methods when a thread is finished and needs to update the UI.
I find it much easier and cleaner.

[quote=241603:@Martin Trippensee]Ok, i had a look to the “UIThreadingWithTask”-Sample in Xojos “examples/Desktop/UpdatingUIFromThread”.

I’ll load a Textfile (different count of lines, up to > 200.000) with ReadAll and split into an Array “Lines” by EndOfLine. The Maximum of UIProgess is “Lines.bound”. Now i have to process every single Line and I would love to update the Progressbars Value only every 100 Steps or so. The Lines should be add into a Listbox after Processing. But if the Textfile > 10000 Lines, the Window freezes…
[/quote]

I didn’t see this at first.

  • Adding 200K records to a ListBox is a bad idea. Especially if you have multiple columns, need to update stuff etc. It’s just too much. Maybe you’re better off using a data-on-demand custom ListBox or if it’s for simple viewing only, store the data in another way and only show what’s needed.