c# - Using ThreadStart my UI stuck -
i'm new threading ui not responding till completion of process. 1 tell me what's wrong here.
reduced code:
static string replacedname = "", replacedwith; private void startprocess(string fpath, string rno) { if (fpath != "" && rno != "") { .... unzip(fpath, path.getdirectoryname(fpath) + "\\" + replacedwith); directoryinfo dir = new directoryinfo(path.getdirectoryname(fpath) + "\\" + replacedwith); foreach (fileinfo file1 in dir.getfiles("*.zip")) { unzip(file1.fullname, path.getdirectoryname(file1.fullname)); file.delete(file1.fullname); } } replacedname = ""; messagebox.show("completed"); } private void button1_click(object sender, eventargs e) { threadstart threadstart = delegate() { startprocess(textbox1.text, textbox2.text); }; threadstart.begininvoke(null, null); } private void unzip(string ziptoextract, string unzipdirectoryl) // extract zip/lot file in same directory { if (this.progressbar1.invokerequired) { progressbar1.invoke(new action(delegate() { unzip(ziptoextract, unzipdirectoryl); })); } else if (this.label3.invokerequired) { label3.invoke(new action(delegate() { unzip(ziptoextract, unzipdirectoryl); })); } else { .... progressbar1.value += 1; progressbar1.refresh(); label3.text = ((progressbar1.value * 100) / (progressbar1.maximum)).tostring(); label3.refresh(); ... } } private void status(string msg, color selccolor) { if (this.richtextbox1.invokerequired) { richtextbox1.invoke(new action(delegate() { status(msg, selccolor); })); } else { richtextbox1.selectionstart = richtextbox1.text.length; var oldcolor = richtextbox1.selectioncolor; richtextbox1.selectioncolor = selccolor; richtextbox1.appendtext(msg + "\n"); richtextbox1.selectioncolor = oldcolor; richtextbox1.refresh(); richtextbox1.scrolltocaret(); } }
replace code
private void button1_click(object sender, eventargs e) { threadstart threadstart = delegate() { startprocess(textbox1.text, textbox2.text); }; threadstart.begininvoke(null, null); }
with
private void button1_click(object sender, eventargs e) { threadstart threadstart = delegate() { startprocess(textbox1.text, textbox2.text); }; thread thread = new thread(threadstart); thread.start(); }
this start new thread delegate assigned.
see here msdn reference.
edit
in unzip
-method doing strange
private void unzip(string ziptoextract, string unzipdirectoryl){ if (this.progressbar1.invokerequired) { progressbar1.invoke(new action(delegate() { unzip(ziptoextract, unzipdirectoryl); })); } else if (this.label3.invokerequired) // why else if on control??? { label3.invoke(new action(delegate() { unzip(ziptoextract, unzipdirectoryl); })); } else { .... progressbar1.value += 1; progressbar1.refresh(); //why use refresh()? label3.text = ((progressbar1.value * 100) / (progressbar1.maximum)).tostring(); label3.refresh(); ... } }
you can't call progressbar1.invokerequired
, if returns false use else if
on label-control! must use separate if/else
every control.
if (this.progressbar1.invokerequired) progressbar1.invoke(new action(delegate() { progressbar1.value++; })); else progressbar1.value++; //pseudo-code
Comments
Post a Comment