c# - How do I update multiple controls on a single form in a multithreaded .NET application? -


i have form has 6 instances of usercontrol spread across 3 tabpages. each usercontrol contains checkedlistbox can contain relatively large number of items (200k+). retrieving , setting data done calling updaterecords in below code:

private void updaterecords() {     list<records> orecords = serverdataaccess.getrecords(m_sconnectionstring);      thread loadrecords1 = new thread(() => usercontrol1.records = orecords);     loadrecords1.start();      thread loadrecords2 = new thread(() => usercontrol2.records = orecords);     loadrecords2.start();      thread loadrecords3 = new thread(() => usercontrol3.records = orecords);     loadrecords3.start(); }  public list<record> records {     set     {         //make deep copy of records changes records in 1 usercontrol not affect others         addrecordstocheckedlistbox(value.asparallel().select(orecord => orecord.clone()).orderby(orecord => orecord.name).toarray());     } }  delegate void addrecordstocheckedlistboxdelegate(record[] records); private void addrecordstocheckedlistbox(record[] records) {     if (invokerequired)     {         invoke(new addrecordstocheckedlistboxdelegate(addrecordstocheckedlistbox), new object[] { records });     }     else     {         clbrecords.suspendlayout();         clbrecords.items.addrange(records);         clbrecords.resumelayout();     } } 

while creating copies of arrays, cpu usage spikes 100%, expected, once threads point start updating controls data arrays, cpu usage pegged @ 50% (dual core system) due needing invoke call, creating bottleneck.

currently, loading data takes approximately 3 minutes, , benchmarking indicated @ minimum, 2:00-2:30 of time spent executing clbrecords.items.addrange(records); in addrecordstocheckedlistbox. each usercontrol functionally isolated each other, changes 1 have no impact of others, do, however, need remain on same form.

is there way load array of records each of these controls takes advantage of having multiple cores available reduce bottleneck?

updating controls on ui thread, use 1 core.

and apart that, achieve efficiency in program should not try parallellism, reducing data. e.g. not fill complete records, id, name. should sufficient ui.

probably need paging, because 200k items in checkedlistbox not user friendly.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -