Friday, 15 May 2015

c# - How to make a Cross-thread Operation using a form textBox thread-safe? -



c# - How to make a Cross-thread Operation using a form textBox thread-safe? -

in c# (visual-studio-express 2013), next code creates error described below:

public void addtoapplog(string formatter, string loggerid, string logtext) { lock(this) { datetime datetime = datetime.now; string logentry = datetime.hour.tostring("00") + ":" + datetime.minute.tostring("00") + ":" + datetime.second.tostring("00") + "." + datetime.millisecond.tostring("000") + " [" + loggerid.padright(18, '·') + "]" + "> " + formatter + logtext + "\n"; applicationlog.appendtext(logentry); } // end of lock } // end of fellow member function: addtoapplog

error:

cross-thread operation not valid: command 'applicationlog' accessed thread other thread created on.

what need ?

use control.invoke:

applicationlog.invoke((methodinvoker)delegate() { applicationlog.appendtext(logentry); });

c# multithreading forms

No comments:

Post a Comment