无效的跨线程访问Backgroundworker错误C# [英] Invalid cross thread access backgroundworker error c#

查看:115
本文介绍了无效的跨线程访问Backgroundworker错误C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.Data.OleDb;
using System.Threading;

namespace SRS
{
    public partial class excelimport : Form
    {
        public excelimport()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog1 = new OpenFileDialog();

            if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox1.Text = openfiledialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if ( backgroundWorker1.IsBusy != true)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        DataTable mysource;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 1; (i <= 10); i++)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                string pathconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + textBox1.Text + " ;Extended Properties=\"Excel 12.0 Xml;HDR=YES;\";";
            OleDbConnection conn = new OleDbConnection(pathconn);

            OleDbDataAdapter mydatapter = new OleDbDataAdapter("select * from [" + textBox2.Text + "$]", conn);
            DataTable dt = new DataTable();

            mydatapter.Fill(dt);
                    datagridview1.datasource=dt;
                    System.Threading.Thread.Sleep(500);
                    worker.ReportProgress((i * 10));
                }
            }


        }


        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.tbprogress.Text = (e.ProgressPercentage.ToString() + "%");
        }

        private delegate void tododelegate();

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                this.tbprogress.Text = "Canceled!";
            }

            else if (!(e.Error == null))
            {
                this.tbprogress.Text = ("Error: " + e.Error.Message);
            }

            else
            {
                this.tbprogress.Text = "Done!";
            }
        }

        private void excelimport_Load(object sender, EventArgs e)
        {
        }


    }
}

我在行中收到错误消息:

I get the error message in line :

datagridview1.datasource = dt;

datagridview1.datasource=dt;

我试图更改这样的代码:

I tried to alter the code like this :

private DataTable loadingtable()
        {
            string pathconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + textBox1.Text + " ;Extended Properties=\"Excel 12.0 Xml;HDR=YES;\";";
            OleDbConnection conn = new OleDbConnection(pathconn);

            OleDbDataAdapter mydatapter = new OleDbDataAdapter("select * from [" + textBox2.Text + "$]", conn);
            DataTable dt = new DataTable();

            mydatapter.Fill(dt);

            return dt;
        }

以及在加载事件中dataGridView1.DataSource = mysource;

and in load event dataGridView1.DataSource = mysource;

以及在bw worker中,mysource = loadingtable();

and in bw worker mysource =loadingtable();

然后就没有错误,但是加载数据需要花费很多时间.

and then there is no error but then load data takes forever.

推荐答案

您无法在非UI线程上更新UI控件.

You can't update UI controls on a non-UI thread.

尝试一下:

datagridview1.Invoke(() => datagridview1.datasource = dt);

这篇关于无效的跨线程访问Backgroundworker错误C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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