c# - Update label from another thread -
this question has reply here:
cross-thread winforms command editing [duplicate] 1 replyi utilize thread writing in class update label. label contents in winform main class.
scanner scanner = new scanner(ref lblcont); scanner.listafile = this.listfiles; thread trd = new thread(new threadstart(scanner.automaticscanner)); trd.isbackground = true; trd.start(); while (!trd.isalive) ; trd.join();
how can see, pass reference of label constructor of sec class. in sec class(scanner) i've method called "automaticscanner" should update label code:
public scanner(ref toolstripstatuslabel _lblcontatore) { lblcounter= _lblcontatore; } thread threadupdatecounter = new thread(new threadstart(this.updatecounter)); threadupdatecounter.isbackground = true; threadupdatecounter.start(); while (!threadupdatecounter .isalive) ; threadupdatecounter.join(); private void aggiornacontatore() { this.lblcounter.text = this.index.tostring(); }
i've receive error on update of label:
cross-thread operation not valid: command 'main' accessed thread other thread created on
i utilize .net 4 winform c#.
thanks lot answers.
news: problem line:
trd.join();
this line block gui , lable not update. there methods command finish of thread , updating label until end? thanks
you cannot update ui other thread other ui thread. utilize update thread on ui thread.
private void aggiornacontatore() { if(this.lblcounter.invokerequired) { this.lblcounter.begininvoke((methodinvoker) delegate() {this.lblcounter.text = this.index.tostring(); ;}); } else { this.lblcounter.text = this.index.tostring(); ; } }
please go through chapter , more book clear image threading:
http://www.albahari.com/threading/part2.aspx#_rich_client_applications
c# multithreading winforms
No comments:
Post a Comment