c# - How can I make a ListView update after each item is added? -
i trying use listview make realtime log output windows form.
this dummy code:
public form1() { initializecomponent(); listview1.view = view.details; listview1.gridlines = false; listview1.scrollable = true; listview1.fullrowselect = true; listview1.columns.add("track"); listview1.columns.add("status"); (int = 1; <= 10000; i++) { listviewitem lvi = new listviewitem("track " + i); lvi.subitems.add("updated"); listview1.items.add(lvi); listview1.topitem = lvi; listview1.ensurevisible(listview1.items.count - 1); } }
how can set refreshes after each line added? @ moment application waits until list generated , loads form complete list.
you can fill data items in thread (for example using task):
application.doevents() ... processes window messages , redraws component.
public form1() { initializecomponent(); listview1.view = view.details; listview1.gridlines = false; listview1.scrollable = true; listview1.fullrowselect = true; listview1.columns.add("track"); listview1.columns.add("status"); task t = new task(new action(() => { refreshlines(); })); t.start(); } public void refreshlines() { if (this.invokerequired) { this.invoke(new methodinvoker(this.refreshlines)); } (int = 1; <= 10000; i++) { listviewitem lvi = new listviewitem("track " + i); lvi.subitems.add("updated"); listview1.items.add(lvi); listview1.topitem = lvi; listview1.ensurevisible(listview1.items.count - 1); application.doevents(); } }
you can call this.refresh(); instead of application.doevents();
Comments
Post a Comment