c# - TextBox text update with MultiThreading -
i'm having quite annoying issue multithreading, i'm updating textbox multilines inside loop this:
if (results.lines.count() != 0) this.invokeex(f => f.results.text += environment.newline); this.invokeex(f => f.results.appendtext("0\t1"));
when run 1 thread, looks ok:
but when run multi threading (10 threads @ same time):
it looks writing in textbox not synchronized, looks messed up.
any way solve this?
this windows forms application .net framework 4.
thanks in advance.
that's because thread wrote new line, might preempted thread write new line.
you should make sure 2 instructions seen atomic operation. i.e., multiple concurrent operations seen if sequential.
you introducing lock around critical region.
private readonly object _lock = new object(); lock(_lock) { if (results.lines.count() != 0) this.invokeex(f => f.results.text += environment.newline); this.invokeex(f => f.results.appendtext("0\t1")); }
Comments
Post a Comment