Prerequisite: When we use Winform to develop, we often encounter: "Inter-thread operation is invalid: it is accessed from a thread that does not create the control "xxxx"."
Cause: The reason for the exception is that winform uses the same thread when rendering the UI interface and operation interface data. When we create another thread and operate the UI control of this thread, it will cause thread insecure. In order to prevent threads from being unsafe, winform eliminates this cross-thread operation and throws exceptions.
Example of exception: Create two controls, one is a button control and the other is a lable control
private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(()=> { if () { (new Action(() => { = "Hello Thread!"; })); } } ); = true; (); }
When executing this button, cross-thread operation exceptions will be caused.
Solution:
Use the InvokeRequired property to determine whether it is thread-safe.
if () { (new Action(() => { = "Hello World!"; })); } else { = "Hello World!"; }
This is the end of this article about the implementation of C# winform cross-thread operation controls. For more related contents of C# winform cross-thread operation controls, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!