Invalid inter-thread operation: access it from a thread that is not the control being created.

Source: Internet
Author: User

Title: There is a button and a lable in the form. Click the button to create a new thread to update the value of lable
1 private void Button1_Click (object sender, EventArgs e) 2         {3             //error: Access it from a thread that is not creating a control 4 thread             t = new Thread (() = > 5             {6 for                 (int i = 0; i < i++) 7                 {8                     this.label1.Text = i.ToString (); 9                 }10             }); 11
   t.start ();         

The above code will make an error and access the other thread's controls from the new thread: this is because. NET prohibits invoking a control across threads, otherwise anyone can manipulate the control, which may eventually cause an error.

1: Invalid inter-thread operation: access it from a thread that is not creating a control. So which thread is the thread that created it?

(Baidu answer) from the programming, only the main thread of the creation interface can access the controls on the interface, so there will be an error.

Of course, setting Checkforillegalcrossthreadcalls =false can remove this check, but this is not a standard practice.
The standard practice is to access the InvokeRequired property of the following form when the interface control is accessed, or to access it directly if false, or to cross-thread access, at which point a delegate is created and invoked by Invoke ().

1 private void Button1_Click (object sender, EventArgs e) 2         {3//             thread dependency: Only threads that create certain objects can access some objects that it creates 4             // Resolves a problem that is called across threads and can be marshaled (Marshal). Call the Invoke Method 5             //Use Marshal 6             new Thread (() = 7             {8 for                 (int i = 0; i < 10000; i++) 9                 {                     action<int> Action = (data) =>11                     {                         this.label1.Text = data. ToString (); };13                         Invoke (action,i);                     }15             }). Start ();             //messagebox does not follow the principle of dependency, so it can be accessed directly in the worker thread.         

2: Why did you use the above code after the error?

2.1 We move the action to the definition discovery

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

2.2 Action is a delegate: then we write a delegate for ourselves (I'm used to writing my own code for knowledge that we don't know)

1//Declare a Delegate object 2 public delegate void Action2<in t> (T t); 3         private void Button1_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);                     (A, I);                 }12             }). Start ();             }15 public         void action2test (int t)-         {             this.label1.Text = t.tostring (); 18         }

2.3 See the code to know: we use the entrusted to complete the task. So how does a delegate play a task?

(because there are some underlying APIs that I don't know, simply say my own understanding): delegates or methods that delegate the method to the thread of the control are executed by it

Invalid inter-thread operation: access it from a thread that is not the control being created.

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.