vb.net - Adding 150,000 records to a listview without freezing UI -


i have listview loop adding 150,000 items listview. testing purposes moved code background worker delegates, still freezes ui. trying find solution can add these items in background while other stuff in app. solutions guys recommend?

this using

   public class form1      private sub button1_click(sender object, e eventargs) handles button1.click         listview1.clear()          listview1.beginupdate()          bw.workerreportsprogress = true         bw.runworkerasync()     end sub      private sub button2_click(sender object, e eventargs) handles button2.click         if bw.isbusy bw.cancelasync()     end sub      private sub bw_dowork(byval sender object, byval e system.componentmodel.doworkeventargs) handles bw.dowork         x = 1 125000             dim lvi new listviewitem("item " & x)             if bw.cancellationpending                 e.cancel = true                 exit             else                 bw.reportprogress(0, lvi)             end if         next     end sub      private sub bw_progresschanged(byval sender object, byval e system.componentmodel.progresschangedeventargs) handles bw.progresschanged         try             dim lvi listviewitem = directcast(e.userstate, listviewitem)             me.listview1.items.add(lvi)         catch ex exception             throw new exception(ex.message)         end try     end sub      private sub bw_runworkercompleted(byval sender object, byval e system.componentmodel.runworkercompletedeventargs) handles bw.runworkercompleted         listview1.endupdate()         if not e.cancelled             debug.print("done")         else             debug.print("cancelled")         end if     end sub end class 

give try, it's great example need... had progress bar shows progress , such, see example image attached. wasn't seeing delegate need perform such operation, mine has 1 required. reason adding items control on ui thread, in order add items need know if invoke required, if invoke otherwise add item control... made thread sleep, can take break; prevents ui wanting lock here , there, it's responsive no freezing.

 option strict on  option explicit on  public class form1  delegate sub setlistitem(byval lstitem listviewitem) 'your delegate..  'start process... private sub btnstartprocess_click(sender object, e eventargs) handles btnstartprocess.click     lvitems.clear()     bwlist.runworkerasync() end sub  private sub addlistitem(byval lstitem listviewitem)     if me.lvitems.invokerequired 'invoke if required...         dim d new setlistitem(addressof addlistitem) 'your delegate...         me.invoke(d, new object() {lstitem})     else 'otherwise, no invoke required...         me.lvitems.items.add(lstitem)     end if end sub  private sub bwlist_dowork(sender object, e system.componentmodel.doworkeventargs) handles bwlist.dowork     dim intcount integer = cint(txtcount.text)     dim dblpercent integer = 100     dim intcomplete integer = 0     dim li listviewitem = nothing      integer = 1 cint(txtcount.text)         if not (bwlist.cancellationpending)             li = new listviewitem             li.text = "item " & i.tostring             addlistitem(li)             threading.thread.sleep(1) 'give thread very..very short break...         elseif (bwlist.cancellationpending)             e.cancel = true             exit         end if          intcomplete = cint(csng(i) / csng(intcount) * 100)         if intcomplete < dblpercent             bwlist.reportprogress(intcomplete)         end if          if li isnot nothing             li = nothing         end if     next  end sub  private sub bwlist_progresschanged(sender object, e system.componentmodel.progresschangedeventargs) handles bwlist.progresschanged     pblist.value = e.progresspercentage end sub  private sub bwlist_runworkercompleted(sender object, e system.componentmodel.runworkercompletedeventargs) handles bwlist.runworkercompleted     if pblist.value < 100 pblist.value = 100     messagebox.show(lvitems.items.count.tostring & " items added!") end sub  private sub btnstopwork_click(sender object, e eventargs) handles btnstopwork.click     bwlist.cancelasync() end sub  private sub btnrestart_click(sender object, e eventargs) handles btnrestart.click     pblist.value = 0     lvitems.items.clear()     txtcount.text = string.empty end sub end class 

screenshot in action...

enter image description here


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -