c# - Create labels in form and update them from background threads -
i have number of background threads (in case 5) generated code below:
progress prog = new progress(); foreach (var questionlang in questionslangconstants.questionlangs.values) { threadpool.queueuserworkitem( delegate { qrepo.uploadquestions(qworkbook.worksheets[questionlang.qsheet], questionlang, prog); }); } prog.show(); progress form has following code in it.
private delegate label addnewlabel(); private delegate void changelabeltext(label lbl, string text); public label addnewlabel() { if (this.invokerequired) { addnewlabel adl = new addnewlabel(addnewlabel); this.invoke(adl); return new label(); } else { label lbl = new label(); this.controls.add(lbl); return lbl; } } public void changelabeltext(label lbl, string text) { if (this.invokerequired) { changelabeltext clt = new changelabeltext(changelabeltext); this.invoke(clt, new object[] { lbl, text }); } else { lbl.text = text; } } and uploadquestions function reads , excel file , uses
label lbl = prog.addnewlabel(); while (questionnum != "") { //code here prog.changelabeltext(lbl, questionnum); //code increment questionnum } i expect 5 labels created , updated number of rows in excel nothing happens. labels not updated created. (even on over other).
i think in method "public label addnewlabel()" in if statement after invoking delegate, returning new label , not original label created in else statement. not sure if problem though.
edit:
instead of "this.invoke(adl);" try "return (this.invoke(adl) label);" , remove "return new label();" statement.
Comments
Post a Comment