如何解决"跨线程操作无效"? [英] How can solve "Cross-thread operation not valid"?

查看:233
本文介绍了如何解决"跨线程操作无效"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试启动多线程,但我不能将它返回到我的错误:跨线程操作无效:'listBox1中线程的创建,控制从另一个线程外部访问是

我的codeS:

 
  公开数据表dTable;
        公开数据表dtRowsCount;
        线T1;
        的ThreadStart TS1;
  无效ExcelToSql()
        {
           // SelectDataFromExcel();
            TS1 =新的ThreadStart(SelectDataFromExcel);
            T1 =新主题(TS1);
            t1.Start();
        }  

 无效SelectDataFromExcel()
        {
            字符串的connectionString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:\来源\ Addresses.xlsx;扩展属性=创先争优12.0; HDR = YES;;
            OleDbConnection的excelConnection =新的OleDbConnection(的connectionString);
                      字符串[]表=新的String [] {Sayfa1};
            excelConnection.Open(); //这个code将打开Excel文件。
            OleDbCommand的的DbCommand;
            OleDbDataAdapter的DataAdapter的;
          // progressBar1.Minimum = 1;

            的foreach(在表功表)
            {
                的DbCommand =新的OleDbCommand(SELECT * FROM [+板+$],excelConnection);
                //progressBar1.Maximum = CountRowsExcel(片).Rows.Count;
               // progressBar2.Value = i + 1的;
                System.Threading.Thread.Sleep(1000);
                ** listBox1.Items.Add(Tablo ISMI:+ sheet.ToUpper()+萨提亚Adeti:+ CountRowsExcel(片).Rows.Count.ToString()+); **
                DataAdapter的=新OleDbDataAdapter的(的DbCommand);
                dTable =新的DataTable();
                DataAdapter.Fill方法(dTable);
                dTable.TableName = sheet.ToUpper();
                dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
                ArrangedDataList(dTable);
                FillSqlTable(dTable,dTable.TableName);
            }

            excelConnection.Close();
            excelConnection.Dispose();
        }  

解决方案

在后台线程不允许访问的UI组件。

在多线程的形式,我有code是这样的:

 私人委托无效InvokeAction();
    私人无效DoUI(InvokeAction调用){
        如果(IsDisposed){
            返回;
        }
        如果(InvokeRequired){
            尝试 {
                调用(呼叫);
            }赶上(InvalidOperationException异常){
                //处理错误
            }
        } 其他 {
            呼叫();
        }
    }
 

然后从我的后台线程我可以code是这样的:

  //东西在其他线程
    myVar的= GetFromDb();
    DoUI(()=> {
       //访问UI组件在这里
       listBox1.Items.Add(myVar的);
    });
    //更多其他线程
 

i try to start multi Thread but i can not it returns to me error: Cross-thread operation not valid: 'listBox1' thread was created to control outside access from another thread was.

MyCodes:

 
  public DataTable dTable;
        public DataTable dtRowsCount;
        Thread t1;
        ThreadStart ts1;
  void ExcelToSql()
        {
           // SelectDataFromExcel();
            ts1 = new ThreadStart(SelectDataFromExcel);
            t1 = new Thread(ts1);
            t1.Start();
        }

   void SelectDataFromExcel()
        {
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Source\Addresses.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
            OleDbConnection excelConnection = new OleDbConnection(connectionString);
                      string[] Sheets = new string[] { "Sayfa1"};
            excelConnection.Open(); // This code will open excel file.            
            OleDbCommand dbCommand;
            OleDbDataAdapter dataAdapter;
          //  progressBar1.Minimum = 1;

            foreach (var sheet in Sheets)
            {
                dbCommand = new OleDbCommand("select * From[" + sheet + "$]", excelConnection);
                //progressBar1.Maximum = CountRowsExcel(sheet).Rows.Count;
               // progressBar2.Value = i + 1;
                System.Threading.Thread.Sleep(1000);
                **listBox1.Items.Add("Tablo ismi: "+sheet.ToUpper()+"Satır Adeti: "+CountRowsExcel(sheet).Rows.Count.ToString()+" ");**
                dataAdapter = new OleDbDataAdapter(dbCommand);
                dTable = new DataTable();
                dataAdapter.Fill(dTable);
                dTable.TableName = sheet.ToUpper();
                dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
                ArrangedDataList(dTable);
                FillSqlTable(dTable, dTable.TableName);
            }

            excelConnection.Close();
            excelConnection.Dispose();
        }

解决方案

The thread in background is not allowed to access the UI components.

In multithreaded forms, I include code like this:

    private delegate void InvokeAction();
    private void DoUI(InvokeAction call) {
        if (IsDisposed) {
            return;
        }
        if (InvokeRequired) {
            try {
                Invoke(call);
            } catch (InvalidOperationException) {
                // Handle error
            }
        } else {
            call();
        }
    }

Then from my background thread I can code like this:

    // Stuff in other thread
    myVar = GetFromDb();
    DoUI(() => { 
       // Access UI components here 
       listBox1.Items.Add(myVar);
    });
    // More other thread 

这篇关于如何解决"跨线程操作无效"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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