|
-
Feb 5th, 2004, 01:08 AM
#1
Thread Starter
Member
Reading Textfile into Listbox is soooo slow
Hi,
I am trying to read a Textfile with 40.000 lines into a listbox
so I am reading each line and add it to the listbox,
but that takes about 15-20 seconds... way to slow
Isn't there a faster way to do it? Cause I found a routine
that only takes 2 seconds to read it into a array, but putting it into
a listbox takes still 15-20 seconds
OK, I have this now and it reads it very fast into s(0)
but still adding to the listviewer takes forever.
Dim s As Array
Dim w As String
Dim f As String = "C:\newsgroups.txt"
Dim stream_reader As New IO.StreamReader(f)
s = (Split(stream_reader.ReadToEnd, vbCrLf))
stream_reader.Close()
' MsgBox("done") This takes only 1 second and
' all the data is in s(0)
Dim a() As String = s(0).Split(Chr(10))
For x = 1 To a.Length - 1
ListView1.Items.Add(a(x))
Next
What am I missing here?
-
Feb 5th, 2004, 02:30 AM
#2
Hi.
I'm not sure it can be done much faster, but if you use the BeginUpdate and EndUpdate it might buy you some time.
That way, it does not redraw the listview every time an item is added.
VB Code:
ListView1.BeginUpdate
Dim a() As String = s(0).Split(Chr(10))
For x = 1 To a.Length - 1
ListView1.Items.Add(a(x))
Next
ListView1.EndUpdate
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Feb 5th, 2004, 03:47 AM
#3
Sleep mode
To enhance it more , place that code in a separate thread . This might not be the speed solution but will prevent any halt or delay that could happen to the UI .
-
Feb 5th, 2004, 08:28 AM
#4
Thread Starter
Member
The listview1.beginupdate and endupdate did the trick. thanks 
And what do you mean with place it in a seperate Thread?
How do you do that?
-
Feb 5th, 2004, 08:40 AM
#5
Sleep mode
I mean multithread your application so that your UI has its own thread and your heavy task has its own thread (which is reading the textfile) .
-
Feb 5th, 2004, 08:42 AM
#6
Frenzied Member
shouldn't he have an Application.DoEvents() in the loop and then include a WithEvents() in there as well? that should force it to react faster. Personnaly, I don't think it gets much faster than that. You're talking about 40 THOUSAND lines of text that your program has to go through.
-
Feb 5th, 2004, 08:48 AM
#7
Sleep mode
Exactly , I'd suggest that too .
-
Feb 5th, 2004, 10:21 AM
#8
Thread Starter
Member
Thanks for the suggestions..
I need to figure out how that works, unless you know how and can tell me about it
-
Feb 5th, 2004, 01:01 PM
#9
Frenzied Member
Code:
For x = 1 To a.Length - 1
application.doevents()
withevents()
ListView1.Items.Add(a(x))
Next
try inserting the red statements into your loop. Let us know if it helps.
-
Feb 5th, 2004, 01:12 PM
#10
DoEvents will actually slow things down because it will pause with every loop interation to allow other processes to run. Also be careful if you use threading here. Threading can become tricky when dealing with UI since the main application thread is the UI thread and objects created on alternate threads can't be shown on the UI thread. Since its just strings it may not matter though. If getting the data or array was the speed bottleneck then threading would be great but if actually loading the strings into the list is the bottleneck then it may not help much.
-
Feb 5th, 2004, 05:13 PM
#11
Thread Starter
Member
Originally posted by thephantom
Code:
For x = 1 To a.Length - 1
application.doevents()
withevents()
ListView1.Items.Add(a(x))
Next
try inserting the red statements into your loop. Let us know if it helps.
The "Withevents()" command doesn't work.
it's expecting a identifier
-
Feb 5th, 2004, 10:40 PM
#12
Sleep mode
Originally posted by Edneeis
DoEvents will actually slow things down because it will pause with every loop interation to allow other processes to run. Also be careful if you use threading here. Threading can become tricky when dealing with UI since the main application thread is the UI thread and objects created on alternate threads can't be shown on the UI thread. Since its just strings it may not matter though. If getting the data or array was the speed bottleneck then threading would be great but if actually loading the strings into the list is the bottleneck then it may not help much.
DoEvents method did fix one problem I had while using special recusive call (threading wouldn't be great idea to use in my situation) though , I used it outside the loop . Umm recursion methods are loop-structured anyway .
-
Feb 6th, 2004, 01:05 AM
#13
DoEvents will make the application 'appear' more responsive in that it will prevent the 'Not Responded' bit but it actually makes the process itself longer because it will pause 40,000 times. The appearance of responsiviness is because it allows the app to redraw and for other things to be performed.
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
|