vb.net - Program freezes when opening a textfile with over 100,000 lines -


my code lets user open text file , contents of textfile per line being placed inside array, program freezes when contents of textfile has on 100,000 lines or more. have tried backgroundworker seems not support openfiledialog.

it runs fine on 1,000 lines or less, need on 100,000 lines of text program. there way tweak performance won't freeze?

here code:

 dim stream system.io.filestream      dim index integer = 0      dim openfiledialog1 new openfiledialog()     openfiledialog1.initialdirectory = "d:\work\base tremble"     openfiledialog1.filter = "txt files (*.txt)|*.txt"     openfiledialog1.filterindex = 2     openfiledialog1.restoredirectory = true      if openfiledialog1.showdialog() = system.windows.forms.dialogresult.ok         try              stream = openfiledialog1.openfile()             if (stream isnot nothing)                  dim sreader new system.io.streamreader(stream)                 while sreader.peek >= 0                     redim preserve earray(index)                     earray(index) = sreader.readline                     richtextbox3.text = earray(index)                     index += 1                      'delay(2)                 loop                 label1.text = "0/" & earray.length & ""             end if         catch ex exception             messagebox.show(ex.message)                     if (stream isnot nothing)                 stream.close()             end if         end try     end if  end sub 

a backgroundworker shouldn't have ui in it.

what need is:

  • prompt filename in main ui
  • create stream in backgroundworker.dowork
  • get rid of array / redim array , use stringcollection - can convery array @ end if want one.
  • raise events through backgroundworker.runworkercompleted

avoid redim preserve if can, it's horrible.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -