c# - Windows Form Embedded Progress Bar Background Worker -
i use progress bar in form having issues.
on form load, progress bar completes , list view populates icons "connected" devices.
when try select index in list of network adapters, progress bar , list view clear exception thrown , nothing happens in gui.
exception thrown: 'system.invalidoperationexception' in system.windows.forms.dll
i'm assuming threading issue have no idea.
here codes, or advice appreciated.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.net.networkinformation; using system.text; using system.threading.tasks; using system.windows.forms; namespace windowsformsapplication1 { public partial class connectdialog : form { backgroundworker bkgndworker = null; dictionary<string, int> dictionary = new dictionary<string, int>(); imagelist deviceimagelist = new imagelist(); public connectdialog() { initializecomponent(); } public connectdialog(list<string> ipaddr) { initializecomponent(); cbonetworkadapter.items.addrange(ipaddr.toarray()); cbonetworkadapter.selectedindex = 0; } private void connectdialog_load(object sender, eventargs e) { bitmap devimage = properties.resources.devicon; deviceimagelist.images.add(devimage); deviceimagelist.imagesize = new size(30, 30); } private void cbonetworkadapter_selectedindexchanged(object sender, eventargs e) { startworking(); } private void startworking() { prgbarconnect.value = 0; dictionary.clear(); lstvwdevids.clear(); bkgndworker = new backgroundworker(); bkgndworker.dowork += new doworkeventhandler(bkgndworker_dowork); bkgndworker.progresschanged += new progresschangedeventhandler(bkgndworker_progresschanged); bkgndworker.runworkercompleted += new runworkercompletedeventhandler(bkgndworker_runworkercompleted); bkgndworker.workerreportsprogress = true; bkgndworker.runworkerasync(); bkgndworker.dispose(); } private void bkgndworker_dowork(object sender, doworkeventargs e) { int numdevs = 0; switch (cbonetworkadapter.selectedindex) { case 0: numdevs = 4; break; case 1: numdevs = 1; break; case 2: numdevs = 10; break; } populateimages(numdevs); } void bkgndworker_progresschanged(object sender, progresschangedeventargs e) { prgbarconnect.value = e.progresspercentage; } void bkgndworker_runworkercompleted(object sender, runworkercompletedeventargs e) { foreach (keyvaluepair<string, int> entry in dictionary) { listviewitem lstviewdevimage = new listviewitem(entry.key, entry.value); lstvwdevids.items.add(lstviewdevimage); } if (lstvwdevids.selecteditems.count > 0) console.writeline(string.format("device id: {0}", lstvwdevids.items.indexof(lstvwdevids.selecteditems[0]))); lstvwdevids.largeimagelist = deviceimagelist; } private void populateimages(int numdevs) { (int = 1; <= numdevs; i++) { system.threading.thread.sleep(100); int percents = (i * 100) / numdevs; string lstvwitemdesc = string.format("dev {0}", - 1); dictionary.add(lstvwitemdesc, deviceimagelist.images.count - 1); bkgndworker.reportprogress(percents, i); } } } }
don't call dispose on backgroundworker want have running. instead:
// if have instance of backgroundworker ... // cancel , dispose 1 if (bkgndworker !=null) { // in case want end dowork nicely bkgndworker.cancelasync(); bkgndworker.dispose(); } // start again bkgndworker = new backgroundworker(); bkgndworker.dowork += new doworkeventhandler(bkgndworker_dowork); bkgndworker.progresschanged += new progresschangedeventhandler(bkgndworker_progresschanged); bkgndworker.runworkercompleted += new runworkercompletedeventhandler(bkgndworker_runworkercompleted); bkgndworker.workerreportsprogress = true; bkgndworker.runworkerasync(); // removed call dispose
you might want implement dispose
on form same cleanup of backgroundworker instance.
Comments
Post a Comment