Invalid inter-thread operation: access it from a thread that is not creating the control "Button1".

Source: Internet
Author: User

The control cannot be accessed across threads after the. Net2. , the control on the form is created by the current main thread, and when the user executes a method asynchronously: Assign a value to the control on the form in the method, remember: When executing an asynchronous delegate, it is actually

is to open a thread to execute that method, which will cause an error: the operation between threads is invalid: it is accessed from a thread that does not create the control "XXX".

In C # WinForm development, this is a more common exception: the Inter-threading operation is not valid, and it is accessed from a thread that does not create the control "XXX". This anomaly comes from. A limitation of NET2: A worker thread cannot access a control created by a window thread. There are two main workarounds, one of which is to set Checkforillegalcrossthreadcalls = False in the window thread, and the other to be cumbersome, invoking the Invoke method in the same way as a delegate.

 Public Form1 ()        {            InitializeComponent ();             false ;        }

But the above is not the recommended method. A better way is to use a delegate to solve

  Private voidButton1_Click (Objectsender, EventArgs e) {            NewAction (show). BeginInvoke (NULL,NULL); }        voidShow () {//asynchronous outside of the method. This form will not feign death             while(true) {Thread.Sleep ( -); Action AC=NewAction (Showtext);  This. Invoke (AC);//implement updating data on a form within a synchronization method            }        }        /// <summary>        ///Update Data/// </summary>        voidShowtext () {Richtextbox1.appendtext ("update \ n"); }

Or use the InvokeRequired property to determine

/*
Summary:
Gets a value that indicates whether the caller must call the Invoke method when making a method call to the control, because the call is in a thread other than the one on which the control is created.
//
return Result:
If the System.Windows.Forms.Control.Handle of the control is created on a different thread than the calling thread (the description you must pass the Invoke
method is called for the control), or false.

Private voidButton1_Click (Objectsender, EventArgs e) {            //new Action (show). BeginInvoke (null, NULL);            NewAction (SHOW1). BeginInvoke (NULL,NULL); }        voidShow1 () { while(true) {Thread.Sleep ( -);//Simulation Wait EffectShow2 (); }        }        voidShow2 () {//description of the current external thread            /*//Summary://Gets a value that indicates whether the caller must call the Invoke method when making a method call to the control, because the call is in a thread other than the one on which the control is created. Returns the result://If the System.Windows.Forms.Control.Handle of the control is created on a different thread than the calling thread (instructions you must pass the Invoke//party             Is true if the control is called), otherwise false. */            if(invokerequired) {/*since it is an external thread, then there is no permission to access the control on the main thread * therefore to the main thread access, open an asynchronous delegate bundle to execute the method * to the main thread execution*/Action AC=NewAction (SHOW2);  This. Invoke (AC);//after execution here. Then the invokerequired is false. Because this is already the main thread accessing the currently created control            }            Else{Richtextbox1.appendtext ("Update 77\n"); }        }

Reference: http://www.cnblogs.com/txw1958/archive/2012/08/21/2649192.html

Invalid inter-thread operation: access it from a thread that is not creating the control "Button1".

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.