Invalidoperationexception未处理 [英] Invalidoperationexception was unhandled

查看:101
本文介绍了Invalidoperationexception未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误说明:

System.Windows.Forms.dll中出现类型为System.InvalidOperationException的未处理异常



其他信息:跨线程操作无效:控制从创建它的线程以外的线程访问。



i我的申请是:



// -------包含来自sql server的数据的类库



Error description:
An unhandled exception of type "System.InvalidOperationException" occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control" accessed from a thread other than the thread it was created on.

i have my application as:

//-------A class library that contains data from sql server

namespace libDataStore
{
    public class csGetData
    {


        static string strCon =                ConfigurationManager.ConnectionStrings["MyCon"].ToString();
        SqlConnection con = new SqlConnection(strCon);
        
        public Single crossEl {get; set;}

        public void getCrossEl()
        {           
            SqlConnection conGetCrossEl = new SqlConnection(strCon);
            conGetCrossEl.Close();
            conGetCrossEl.Open();
            SqlCommand cmd1 = new SqlCommand("usp_getCrossEl", conGetCrossEl);
            cmd1.CommandType = CommandType.StoredProcedure;
            try
            {
                SqlDataReader dr = cmd1.ExecuteReader();
                dr.Read(); 
                crossEl = 0;
                crossEl = Convert.ToSingle(dr[0].ToString());                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());                
            }            
            conGetCrossEl.Close();
            conGetCrossEl.Dispose();
        }
         

    }

}





// -------------另一个包含面板的类库







//------------- Another Class library which contain panels


namespace libPanels
{   
    public class csCrossEl   //from ClassLibrary libPanels

    {                       
        Label lblCrossElHeading = new Label();
        Label lblCrossEl = new Label();
        TextBox txtCrossEl = new TextBox();
        Label lblCrossElNew = new Label();
        TextBox txtCrossElNew = new TextBox();
        Button btnCrossElReset = new Button();
        Button btnCrossElSave = new Button();
        Button btnCrossElCancel = new Button();

        FlowLayoutPanel dynPnlCrossEl = new FlowLayoutPanel();
        public void createPanelCrossEl()
        {
            //Panel dynamicPanelEl = new Panel();
            dynPnlCrossEl.Location = new System.Drawing.Point(0, 27);
            dynPnlCrossEl.Size = new Size(741, 416);
            dynPnlCrossEl.Name = "panelCrossEl";

            //Label lblCrossElHeading = new Label();
            lblCrossElHeading.Location = new Point(330, 34);
            lblCrossElHeading.Size = new Size(125, 13);
            lblCrossElHeading.Text = "Cross Elasticity";
            lblCrossElHeading.Font = new Font(lblCrossElHeading.Font, FontStyle.Bold);

            //Label lblCrossEl = new Label();
            lblCrossEl.Location = new Point(164, 132);
            lblCrossEl.Size = new Size(175, 13);
            lblCrossEl.Text = "Cross Elasticity:";

            //TextBox txtCrossEl = new TextBox();
            txtCrossEl.Location = new Point(379, 129);
            txtCrossEl.Size = new Size(156, 20);
            txtCrossEl.Enabled = false;

            //Label lblCrossElNew = new Label();
            lblCrossElNew.Location = new Point(159, 252);
            lblCrossElNew.Size = new Size(175, 13);
            lblCrossElNew.Text = "New Cross Elasticity :";

            //TextBox txtCrossElNew = new TextBox();
            txtCrossElNew.Location = new Point(379, 249);
            txtCrossElNew.Size = new Size(156, 20);

            //Button btnCrossElReset = new Button();
            btnCrossElReset.Location = new Point(153, 365);
            btnCrossElReset.Size = new Size(93, 23);
            btnCrossElReset.Text = "Reset";
            btnCrossElReset.Click += new EventHandler(this.btnCrossElReset_Click);

            //Button btnCrossElSave = new Button();
            btnCrossElSave.Location = new Point(379, 365);
            btnCrossElSave.Size = new Size(93, 23);
            btnCrossElSave.Text = "Save";
            btnCrossElSave.Click += new EventHandler(btnCrossElSave_Click);

            //Button btnCrossElCancel = new Button();
            btnCrossElCancel.Location = new Point(603, 365);
            btnCrossElCancel.Size = new Size(93, 23);
            btnCrossElCancel.Text = "Cancel";
            btnCrossElCancel.Click += new EventHandler(btnCrossElCancel_Click);

            
            dynPnlCrossEl.Controls.Add(lblCrossElHeading);
            dynPnlCrossEl.Controls.Add(lblCrossEl);
            dynPnlCrossEl.Controls.Add(txtCrossEl);
            dynPnlCrossEl.Controls.Add(lblCrossEl);
            dynPnlCrossEl.Controls.Add(txtCrossEl);
            dynPnlCrossEl.Controls.Add(btnCrossElReset);
            dynPnlCrossEl.Controls.Add(btnCrossElSave);
            dynPnlCrossEl.Controls.Add(btnCrossElCancel);

            //this.Controls.Add(dynPnlCrossEl);
            Form.ActiveForm.Controls.Add(dynPnlCrossEl);    //This line causes stated error

        }

        public void addFunctionality()
        {
            csGetData obdata = new csGetData();
            obdata.getCrossEl();
            txtCrossEl.Text = Convert.ToString(obdata.crossEl);

        }

        protected void btnCrossElReset_Click(object sender, EventArgs e)
        {
            txtCrossEl.Clear();
        }

        protected void btnCrossElSave_Click(object sender, EventArgs e)
        {
            if (txtCrossEl.Text == "")
            { MessageBox.Show("Please fill Proper Values.."); }
            else
            {
                try
                {
                    //do_something
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }

        }

        protected void btnCrossElCancel_Click(object sender, EventArgs e)
        {
            //HideAllPanel();
            //dynamicPanelWelcome.Visible = true;
        }
    }
}







//并显示:






//and this to display :

namespace MyProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();          
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            csCrossEl obCrossEl = new csCrossEl();            
            obCrossEl.addFunctionality();
            obCrossEl.createPanelCrossEl();
        }
    }
}





我尝试了什么:



我有这个应用程序还有两个类库,其中一个保存数据,第二个创建面板

t

i认为问题出现coz of panel类倾向于在活动表单上显示数据

活动表单是一个线程,类库是其他两个线程吗?

如何解决?



What I have tried:

I have this application with two more class libraries, in which one hold data and second create panel
t
i think problem arise coz of panel Class tends to display data on active form
active form is one thread and class libraries are other two threads?
how to solve it??

推荐答案

参见 Control.InvokeRequired属性(System.Windows.Forms) [ ^ ]。


这篇关于Invalidoperationexception未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆