i have form publish button, cancel button, label, , progress bar. when click button, files needed copied different directory gets copied over. part works fine. i'm utilizing progress bar let user know file being published @ given time. way, if goes wrong or file big, user knows file causing problem (via label). files populated through list of strings. originally, using thread.sleep make process longer split second of copying files. however, using prevents interaction ui (such if user wants cancel). i'm using backgroundworker accomplish problem.
what expect happen when clicking "publish"
label tells me "starting publication...". after each file, progressbar value increments, label indicates file that's being published, when copying over, label says "publish complete", progressbar value maxed.
what doesn't happen
the label doesn't update files. says "starting publication" "publish complete".
question
how "reportprogress" correctly can update label after each file being copied?
code
private void btnpublish_click(object sender, eventargs e) { try { progressbar(); } catch (exception ex) { data.common.errorhandling.displayerror(ex); } } private void progressbar() { lblpublish.text = "starting publication..."; //dowork if (!backgroundworker1.isbusy) { backgroundworker1.runworkerasync(); } } private void btncancel_click(object sender, eventargs e) { if (backgroundworker1.isbusy) { backgroundworker1.cancelasync(); } } private void backgroundworker1_dowork(object sender, doworkeventargs e) { list<string> allacros = numoftotalacros(); foreach (var acro in allacros) { backgroundworker1.reportprogress(allacros.indexof(acro)); if (backgroundworker1.cancellationpending) { e.cancel = true; backgroundworker1.reportprogress(0); return; } thread.sleep(500); } } private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) { list<string> acros = new list<string>(); cdspublishbar.value = e.progresspercentage; (int = 0; < acros.count; i++) { lblpublish.text = "processing " + acros[i].tostring() + "..."; } } private void backgroundworker1_runworkercompleted(object sender, runworkercompletedeventargs e) { if (e.cancelled) { lblpublish.text = "processing cancelled"; } else if (e.error != null) { lblpublish.text = e.error.message; } else { cdspublishbar.value = 100; lblpublish.text = "publish complete"; } }
you're creating new empty collection has no relation collection in dowork event, , iterating on it... never print anything.
// empty list list<string> acros = new list<string>(); // no elements iterate on (int = 0; < acros.count; i++) { lblpublish.text = "processing " + acros[i].tostring() + "..."; } instead, pass values need via reportprogress's second parameter:
backgroundworker1.reportprogress(0, acro); and use in progresschanged method:
cdspublishbar.value = e.progresspercentage; lblpublish.text = string.format("processing {0}...", e.userstate);
Comments
Post a Comment