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:

enter image description here

but when run multi threading (10 threads @ same time):

enter image description here

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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -