Results 1 to 13 of 13

Thread: Reading Textfile into Listbox is soooo slow

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    58

    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?

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    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:
    1. ListView1.BeginUpdate
    2.  
    3. Dim a() As String = s(0).Split(Chr(10))
    4. For x = 1 To a.Length - 1
    5. ListView1.Items.Add(a(x))
    6. Next
    7.  
    8. ListView1.EndUpdate
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    58
    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?

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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) .

  6. #6
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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.

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Exactly , I'd suggest that too .

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    58
    Thanks for the suggestions..
    I need to figure out how that works, unless you know how and can tell me about it

  9. #9
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    58
    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

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width