The operation performed on the control is being invoked from the wrong thread. Use Control.Invoke or Control.BeginInvoke to marshal to the correct thread to perform this operation. (To resolve)

Source: Internet
Author: User
If you are interested in this article, you may have encountered the following similar exception hints in programming:
1, the thread operation is invalid: it is accessed from a thread that is not creating the control "X". (x is the name of the control)
2, "the operation performed on the control is being invoked from the wrong thread." Use Control.Invoke or Control.BeginInvoke to marshal to the correct thread to perform this operation. "

Seeing this hint is most likely caused by a control that is accessed across threads. As a small example,

The code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Threading;

Namespace WindowsFormsApplication6
{
public partial class Form1:form
{
Public Form1 ()

{
InitializeComponent ();
}

public delegate void MyMethod (); Declares a delegate so that other threads access the

Button
private void Button1_Click (object sender, EventArgs e)
{
Thread AAA = new Thread (new ThreadStart (Opendialogonotherthread));
Aaa. Start ();
}

Other Threads Open dialog box
void Opendialogonotherthread ()
{
MyMethod OpenDialog = new MyMethod (this. OpenDialog);
This.  Invoke (OpenDialog); On the current thread, call OpenDialog
}

Open a dialog box
void OpenDialog ()
{
OpenFileDialog OpenFileDialog = new OpenFileDialog ();
Openfiledialog.showdialog ();
}
}
}


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.