跨线程操作无效:控制“dataGridView1"从创建它的线程以外的线程访问 [英] Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on

查看:33
本文介绍了跨线程操作无效:控制“dataGridView1"从创建它的线程以外的线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 表单,它需要很长时间才能将数据加载到我的 datagridview 中.

我不断收到此行中的错误:

 dataGridView1.Rows.Add(row);

跨线程操作无效:控件dataGridView1"是从创建它的线程以外的线程访问的.

这是我的代码:

 public List_checked = new List();私有无效 getBarcodProperty(){string query = "select Name from RfidUnic where Barcode = @barcode";而(真){while (_checked.Count > 0){SQLiteCommand cmd = new SQLiteCommand(query, sqliteConnection);cmd.Parameters.AddWithValue("@barcode", _checked[0]);sqliteConnection.Open();SQLiteDataReader da = cmd.ExecuteReader();如果(da.Read()){如果 (!da.IsDBNull(0)){string[] row = new string[] { da[0].ToString(), "1", _checked[0] };dataGridView1.Rows.Add(row);_checked.RemoveAt(0);}}别的{string[] row = new string[] { "empty", "1", _checked[0] };dataGridView1.Rows.Add(row);_checked.RemoveAt(0);}sqliteConnection.Close();}}}

请告诉我哪里出错了

谢谢

解决方案

您的getBarcodProperty"函数是从另一个线程而不是从 UI 线程调用的,并且您尝试在该方法中访问 datagridview,因此产生了问题.

>

有两种解决问题的方法.

  1. 在您的加载方法RFID_Load(object sender, EventArgs e)"中,将此行添加为第一行.(在这种情况下,您不必更改任何代码)

<块引用>

Control.CheckForIllegalCrossThreadCalls = false;

更多细节:http://dotnetstep.blogspot.in/2009/09/cross-thread-operation-not-valid.html

  1. 另一种解决方案是使用委托.

<块引用>

dataGridView1.Invoke(new Action(() =>{dataGridView1.Rows.Add(row);}));

注意:对于每次调用,您必须执行相同的操作.

I have a Windows form which is taking forever to load data into my datagridview.

I keep getting the error in this line:

    dataGridView1.Rows.Add(row);

Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on.

Here is my code:

    public List<string> _checked = new List<string>();

    private void getBarcodProperty()
    {                
        string query = "select Name from RfidUnic where Barcode = @barcode";

        while (true)
        {
            while (_checked.Count > 0)
            {
                SQLiteCommand cmd = new SQLiteCommand(query, sqliteConnection);
                cmd.Parameters.AddWithValue("@barcode", _checked[0]);
                sqliteConnection.Open();

                SQLiteDataReader da = cmd.ExecuteReader();

                if (da.Read())
                {
                    if (!da.IsDBNull(0))
                    {
                        string[] row = new string[] { da[0].ToString(), "1", _checked[0] };
                        dataGridView1.Rows.Add(row);
                        _checked.RemoveAt(0);
                    }
                }
                else
                {
                    string[] row = new string[] { "empty", "1", _checked[0] };
                    dataGridView1.Rows.Add(row);
                    _checked.RemoveAt(0);
                }

                sqliteConnection.Close();
            }
        }
    }

Please show me where I am going wrong

Thanks

解决方案

Your "getBarcodProperty" function is being called from another thread and not from UI thread and you are try to access datagridview in that method so it created problem.

There are two ways two solve problem.

  1. In your load method "RFID_Load(object sender, EventArgs e)" add this line as first line. ( In this case you don't have to change any of your code)

Control.CheckForIllegalCrossThreadCalls = false;

More detail : http://dotnetstep.blogspot.in/2009/09/cross-thread-operation-not-valid.html

  1. Another solution is to use delegate.

dataGridView1.Invoke(new Action(() => { dataGridView1.Rows.Add(row); }));

Note: For each call you have to do same thing.

这篇关于跨线程操作无效:控制“dataGridView1"从创建它的线程以外的线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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