The Inter-thread operation is invalid: It is never accessed by the thread that is not creating the control ., Thread control

Source: Internet
Author: User

The Inter-thread operation is invalid: It is never accessed by the thread that is not creating the control ., Thread control
Question: There is a button and a lable in the form. Click the button to create a new thread to update the lable value.

1 private void button#click (object sender, EventArgs e) 2 {3 // error: access it from a Thread not creating a control 4 Thread t = new Thread (() => 5 {6 for (int I = 0; I <100; I ++) 7 {8 this. label1.Text = I. toString (); 9} 10}); 11 t. start (); 12}

The code above will report an error and access the controls of other threads from the new thread: this is because. NET prohibits cross-thread calls of controls. Otherwise, anyone can operate on the controls and may eventually cause errors.

1: The Inter-thread operation is invalid: access it from a thread that is not a control creation. Which thread is used to create it?

(Baidu answer) in terms of program design, only the main thread that creates the interface can access the controls on the interface, so errors may occur.

Of course, setting checkforillegalcrossthreadcils = false can remove this check, but this is not a standard practice.
The standard practice is to access the InvokeRequired attribute of the form when accessing the interface control. If it is false, it can be accessed directly; otherwise, it is accessed across threads. At this time, create a delegate, and call it through Invoke.

1 private void button#click (object sender, EventArgs e) 2 {3 // thread dependency: only the thread that creates some objects can access some objects it creates. 4 // solve the problem of cross-thread calling. You can use envelope Al ). call the Invoke Method 5 // use the mail 6 new Thread () => 7 {8 for (int I = 0; I <10000; I ++) 9 {10 Action <int> action = (data) => 11 {12 this. label1.Text = data. toString () ;}; 13 Invoke (action, I); 14} 15 }). start (); 16 // MessageBox does not follow the dependency principle, so you can directly access 17 in the work thread}

 

2: Why is no error reported after the above code is used?

2.1 Convert Action to definition discovery

1 namespace System 2 {3 // Summary: 4 // encapsulate a method that has only one parameter and does not return a value. 5 // 6 // parameter: 7 // obj: 8 // parameters of the method encapsulated by this delegate. 9 // 10 // type parameter: 11 // T: 12 // The parameter type of the method encapsulated by this delegate. 13 public delegate void Action <in T> (T obj); 14}

2.2 Action is a delegate: Then we write a delegate by ourselves (I am used to writing code for my unmastered knowledge)

1 // declare a delegate object 2 public delegate void Action2 <in T> (T t); 3 private void button#click (object sender, EventArgs e) 4 {5 new Thread (() => 6 {7 for (int I = 0; I <10000; I ++) 8 {9 Action2 <int> a = new Action2 <int> (Action2Test ); 10 Invoke (a, I); 11} 12 }). start (); 13 14} 15 public void Action2Test (int t) 16 {17 this. label1.Text = t. toString (); 18}

2.3 check the code and find out that we use a delegate to complete the task. So how does a delegate work as a task?

(Because it contains some underlying APIs that I don't understand, let's simply put it into my own understanding): Delegate the method to the delegate or method of the thread where the control is located for execution.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.