iTEgg
iTEgg

Reputation: 8342

C# Invoke Action Cross Thread Access

greetings, im new to programming.

at the moment my application uses delegates to process/execute methods that reside in a another class/object.

but i was getting an error stating that they were residing in separte threads. so after searching the web i came up with this:

this.Invoke(new Action(delegate() { this.ChatRichTextBox.AppendText(EventArgs.commMessage); }));

this has worked fine accept i have no idea whats going on. i just pasted the code and it worked.

at present now though i have a need for doing this differently as the compiler says it cant do > < != operations within this action.

please advise on how best to implement this?

thank you very much.

Upvotes: 1

Views: 8600

Answers (1)

Miguel Sevilla
Miguel Sevilla

Reputation: 466

Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible, such as race conditions and deadlocks. It is important to make sure that access to your controls is performed in a thread-safe way.

It is unsafe to call a control from a thread other than the one that created the control without using the Invoke method.

Upvotes: 4

Related Questions